root/src/xdisp.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. it_char_has_category
  2. char_can_wrap_before
  3. char_can_wrap_after
  4. fill_column_indicator_column
  5. redisplay_other_windows
  6. wset_redisplay
  7. fset_redisplay
  8. bset_redisplay
  9. bset_update_mode_line
  10. wset_update_mode_line
  11. ATTRIBUTE_FORMAT_PRINTF
  12. ATTRIBUTE_FORMAT_PRINTF
  13. window_text_bottom_y
  14. window_box_width
  15. window_box_height
  16. window_box_left_offset
  17. window_box_right_offset
  18. window_box_left
  19. window_box_right
  20. window_box
  21. window_box_edges
  22. line_bottom_y
  23. DEFUN
  24. default_line_pixel_height
  25. string_from_display_spec
  26. window_hscroll_limited
  27. reset_box_start_end_flags
  28. pos_visible_p
  29. check_char_and_length
  30. string_pos_nchars_ahead
  31. string_pos
  32. c_string_pos
  33. number_of_chars
  34. compute_string_pos
  35. estimate_mode_line_height
  36. pixel_to_glyph_coords
  37. x_y_to_hpos_vpos
  38. frame_to_window_pixel_xy
  39. get_glyph_string_clip_rects
  40. get_glyph_string_clip_rect
  41. get_phys_cursor_geometry
  42. remember_mouse_glyph
  43. adjust_window_ends
  44. hscrolling_current_line_p
  45. safe_eval_handler
  46. safe__call
  47. safe_call
  48. safe_call1
  49. safe__call1
  50. safe_eval
  51. safe__eval
  52. safe_call2
  53. CHECK_IT
  54. CHECK_WINDOW_END
  55. init_iterator
  56. get_narrowed_width
  57. get_narrowed_len
  58. get_medium_narrowing_begv
  59. get_medium_narrowing_zv
  60. get_nearby_bol_pos
  61. get_small_narrowing_begv
  62. get_large_narrowing_begv
  63. get_large_narrowing_zv
  64. unwind_narrowed_begv
  65. start_display
  66. in_ellipses_for_invisible_text_p
  67. init_from_display_pos
  68. init_to_row_start
  69. init_to_row_end
  70. handle_stop
  71. compute_stop_pos
  72. compute_display_string_pos
  73. compute_display_string_end
  74. handle_fontified_prop
  75. face_at_pos
  76. handle_face_prop
  77. underlying_face_id
  78. face_before_or_after_it_pos
  79. handle_invisible_prop
  80. setup_for_ellipsis
  81. find_display_property
  82. get_display_property
  83. display_min_width
  84. handle_display_prop
  85. handle_display_spec
  86. display_prop_end
  87. handle_single_display_spec
  88. display_prop_intangible_p
  89. single_display_spec_string_p
  90. display_prop_string_p
  91. string_buffer_position_lim
  92. string_buffer_position
  93. handle_composition_prop
  94. handle_overlay_change
  95. next_overlay_string
  96. compare_overlay_entries
  97. load_overlay_strings
  98. get_overlay_strings_1
  99. get_overlay_strings
  100. push_it
  101. iterate_out_of_display_property
  102. restore_face_box_flags
  103. pop_it
  104. back_to_previous_line_start
  105. strings_with_newlines
  106. forward_to_next_line_start
  107. back_to_previous_visible_line_start
  108. reseat_at_previous_visible_line_start
  109. reseat_at_next_visible_line_start
  110. reseat
  111. reseat_1
  112. reseat_to_string
  113. lookup_glyphless_char_display
  114. merge_escape_glyph_face
  115. merge_glyphless_glyph_face
  116. forget_escape_and_glyphless_faces
  117. get_next_display_element
  118. set_iterator_to_next
  119. next_element_from_display_vector
  120. get_visually_first_element
  121. next_element_from_string
  122. next_element_from_c_string
  123. next_element_from_ellipsis
  124. next_element_from_image
  125. next_element_from_xwidget
  126. next_element_from_stretch
  127. compute_stop_pos_backwards
  128. handle_stop_backwards
  129. next_element_from_buffer
  130. next_element_from_composition
  131. move_it_in_display_line_to
  132. move_it_in_display_line
  133. move_it_to
  134. move_it_vertically_backward
  135. move_it_vertically
  136. move_it_past_eol
  137. move_it_by_lines
  138. partial_line_height
  139. fast_move_it_horizontally
  140. in_display_vector_p
  141. window_text_pixel_size
  142. DEFUN
  143. format_nargs
  144. add_to_log
  145. vadd_to_log
  146. message_log_maybe_newline
  147. message_dolog
  148. message_log_check_duplicate
  149. message3
  150. message_to_stderr
  151. message3_nolog
  152. message1
  153. message1_nolog
  154. message_with_string
  155. ATTRIBUTE_FORMAT_PRINTF
  156. message
  157. update_echo_area
  158. ensure_echo_area_buffers
  159. with_echo_area_buffer
  160. with_echo_area_buffer_unwind_data
  161. unwind_with_echo_area_buffer
  162. setup_echo_area_for_printing
  163. display_echo_area
  164. display_echo_area_1
  165. resize_echo_area_exactly
  166. resize_mini_window_1
  167. resize_mini_window
  168. current_message
  169. current_message_1
  170. push_message
  171. restore_message
  172. pop_message_unwind
  173. check_message_stack
  174. clear_message_stack
  175. truncate_echo_area
  176. truncate_message_1
  177. set_message
  178. set_message_1
  179. clear_message
  180. clear_garbaged_frames
  181. echo_area_display
  182. window_buffer_changed
  183. mode_line_update_needed
  184. window_frozen_p
  185. format_mode_line_unwind_data
  186. unwind_format_mode_line
  187. store_mode_line_noprop_char
  188. store_mode_line_noprop
  189. gui_consider_frame_title
  190. needs_no_redisplay
  191. prepare_menu_bars
  192. update_menu_bar
  193. restore_selected_window
  194. restore_frame_selected_window
  195. update_tab_bar
  196. display_tab_bar
  197. build_desired_tab_bar_string
  198. display_tab_bar_line
  199. tab_bar_height
  200. redisplay_tab_bar
  201. tab_bar_item_info
  202. get_tab_bar_item
  203. handle_tab_bar_click
  204. note_tab_bar_highlight
  205. tty_get_tab_bar_item
  206. tty_handle_tab_bar_click
  207. update_tool_bar
  208. build_desired_tool_bar_string
  209. display_tool_bar_line
  210. tool_bar_height
  211. redisplay_tool_bar
  212. tool_bar_item_info
  213. get_tool_bar_item
  214. handle_tool_bar_click_with_device
  215. handle_tool_bar_click
  216. note_tool_bar_highlight
  217. hscroll_window_tree
  218. hscroll_windows
  219. debug_method_add
  220. text_outside_line_unchanged_p
  221. redisplay
  222. overlay_arrow_string_or_property
  223. overlay_arrow_in_current_buffer_p
  224. overlay_arrows_changed_p
  225. update_overlay_arrows
  226. overlay_arrow_at_row
  227. check_point_in_composition
  228. reconsider_clip_changes
  229. propagate_buffer_redisplay
  230. redisplay_internal
  231. unwind_redisplay_preserve_echo_area
  232. redisplay_preserve_echo_area
  233. unwind_redisplay
  234. unwind_display_working_on_window
  235. mark_window_display_accurate_1
  236. mark_window_display_accurate
  237. disp_char_vector
  238. block_buffer_flips
  239. unblock_buffer_flips
  240. buffer_flipping_blocked_p
  241. redisplay_windows
  242. redisplay_window_error
  243. redisplay_window_0
  244. redisplay_window_1
  245. update_redisplay_ticks
  246. set_cursor_from_row
  247. run_window_scroll_functions
  248. cursor_row_fully_visible_p
  249. try_scrolling
  250. compute_window_start_on_continuation_line
  251. try_cursor_movement
  252. set_vertical_scroll_bar
  253. set_horizontal_scroll_bar
  254. window_start_acceptable_p
  255. DEFUN
  256. redisplay_window
  257. try_window
  258. try_window_reusing_current_matrix
  259. find_last_row_displaying_text
  260. find_last_unchanged_at_beg_row
  261. find_first_unchanged_at_end_row
  262. sync_frame_with_window_matrix_rows
  263. row_containing_pos
  264. try_window_id
  265. dump_glyph_row
  266. dump_glyph
  267. dump_glyph_row
  268. DEFUN
  269. DEFUN
  270. DEFUN
  271. get_overlay_arrow_glyph_row
  272. insert_left_trunc_glyphs
  273. row_hash
  274. compute_line_metrics
  275. clear_position
  276. append_space_for_newline
  277. extend_face_to_end_of_line
  278. trailing_whitespace_p
  279. highlight_trailing_whitespace
  280. row_for_charpos_p
  281. cursor_row_p
  282. push_prefix_prop
  283. get_it_property
  284. get_line_prefix_it_property
  285. handle_line_prefix
  286. unproduce_glyphs
  287. find_row_edges
  288. display_count_lines_logically
  289. display_count_lines_visually
  290. maybe_produce_line_number
  291. should_produce_line_number
  292. row_text_area_empty
  293. display_line
  294. DEFUN
  295. DEFUN
  296. DEFUN
  297. display_menu_bar
  298. deep_copy_glyph_row
  299. display_tty_menu_item
  300. redisplay_mode_lines
  301. display_mode_lines
  302. display_mode_line
  303. move_elt_to_front
  304. safe_set_text_properties
  305. display_mode_element
  306. store_mode_line_string
  307. pint2str
  308. pint2hrstr
  309. decode_mode_spec_coding
  310. percent99
  311. decode_mode_spec
  312. count_lines
  313. display_count_lines
  314. display_string
  315. invisible_prop
  316. DEFUN
  317. calc_pixel_width_or_height
  318. get_font_ascent_descent
  319. dump_glyph_string
  320. init_glyph_string
  321. append_glyph_string_lists
  322. prepend_glyph_string_lists
  323. append_glyph_string
  324. get_char_face_and_encoding
  325. get_glyph_face_and_encoding
  326. get_char_glyph_code
  327. fill_composite_glyph_string
  328. fill_gstring_glyph_string
  329. fill_glyphless_glyph_string
  330. fill_glyph_string
  331. fill_image_glyph_string
  332. fill_xwidget_glyph_string
  333. fill_stretch_glyph_string
  334. get_per_char_metric
  335. normal_char_ascent_descent
  336. normal_char_height
  337. gui_get_glyph_overhangs
  338. left_overwritten
  339. left_overwriting
  340. right_overwritten
  341. right_overwriting
  342. set_glyph_string_background_width
  343. glyph_string_containing_background_width
  344. compute_overhangs_and_x
  345. draw_glyphs
  346. font_for_underline_metrics
  347. append_glyph
  348. append_composite_glyph
  349. take_vertical_position_into_account
  350. produce_image_glyph
  351. produce_xwidget_glyph
  352. append_stretch_glyph
  353. produce_stretch_glyph
  354. produce_special_glyphs
  355. pad_mode_line
  356. calc_line_height_property
  357. append_glyphless_glyph
  358. produce_glyphless_glyph
  359. gui_produce_glyphs
  360. gui_write_glyphs
  361. gui_insert_glyphs
  362. gui_clear_end_of_line
  363. get_specified_cursor_type
  364. set_frame_cursor_types
  365. get_window_cursor_type
  366. notice_overwritten_cursor
  367. gui_fix_overlapping_area
  368. draw_phys_cursor_glyph
  369. erase_phys_cursor
  370. display_and_set_cursor
  371. update_window_cursor
  372. update_cursor_in_window_tree
  373. gui_update_cursor
  374. gui_clear_cursor
  375. draw_row_with_mouse_face
  376. show_mouse_face
  377. clear_mouse_face
  378. coords_in_mouse_face_p
  379. cursor_in_mouse_face_p
  380. rows_from_pos_range
  381. mouse_face_from_buffer_pos
  382. fast_find_string_pos
  383. mouse_face_from_string_pos
  384. on_hot_spot_p
  385. find_hot_spot
  386. define_frame_cursor1
  387. note_mode_line_or_margin_highlight
  388. note_mouse_highlight
  389. gui_clear_window_mouse_face
  390. cancel_mouse_face
  391. expose_area
  392. expose_line
  393. expose_overlaps
  394. phys_cursor_in_rect_p
  395. gui_draw_vertical_border
  396. gui_draw_right_divider
  397. gui_draw_bottom_divider
  398. expose_window
  399. expose_window_tree
  400. expose_frame
  401. gui_intersect_rectangles
  402. syms_of_xdisp
  403. init_xdisp
  404. show_hourglass
  405. start_hourglass
  406. cancel_hourglass
  407. adjust_glyph_width_for_mouse_face
  408. get_cursor_offset_for_mouse_face

     1 /* Display generation from window structure and buffer text.
     2 
     3 Copyright (C) 1985-2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
    21 
    22    Redisplay.
    23 
    24    Emacs separates the task of updating the display -- which we call
    25    "redisplay" -- from the code modifying global state, e.g. buffer
    26    text.  This way functions operating on buffers don't also have to
    27    be concerned with updating the display as result of their
    28    operations.
    29 
    30    Redisplay is triggered by the Lisp interpreter when it decides it's
    31    time to do it.  This is done either automatically for you as part
    32    of the interpreter's command loop, or as the result of calling Lisp
    33    functions like `sit-for'.  The C function `redisplay_internal' in
    34    xdisp.c is the only entry into the inner redisplay code.
    35 
    36    The following diagram shows how redisplay code is invoked.  As you
    37    can see, Lisp calls redisplay and vice versa.
    38 
    39    Under window systems like X, some portions of the redisplay code
    40    are also called asynchronously, due to mouse movement or expose
    41    events.  "Asynchronously" in this context means that any C function
    42    which calls maybe_quit or process_pending_signals could enter
    43    redisplay via expose_frame and/or note_mouse_highlight, if X events
    44    were recently reported to Emacs about mouse movements or frame(s)
    45    that were exposed.  And such redisplay could invoke the Lisp
    46    interpreter, e.g. via the :eval forms in mode-line-format, and as
    47    result the global state could change.  It is therefore very
    48    important that C functions which might cause such "asynchronous"
    49    redisplay, but cannot tolerate the results, use
    50    block_input/unblock_input around code fragments which assume that
    51    global Lisp state doesn't change.  If you don't follow this rule,
    52    you will encounter bugs which are very hard to explain.  One place
    53    that needs to take such precautions is timer_check, some of whose
    54    code cannot tolerate changes in timer alists while it processes
    55    timers.
    56 
    57    +--------------+   redisplay     +----------------+
    58    | Lisp machine |---------------->| Redisplay code |<--+
    59    +--------------+   (xdisp.c)     +----------------+   |
    60           ^                                  |           |
    61           +----------------------------------+           |
    62             Block input to prevent this when             |
    63             called asynchronously!                       |
    64                                                          |
    65                     note_mouse_highlight (asynchronous)  |
    66                                                          |
    67                                     X mouse events  -----+
    68                                                          |
    69                             expose_frame (asynchronous)  |
    70                                                          |
    71                                    X expose events  -----+
    72 
    73    What does redisplay do?  Obviously, it has to figure out somehow what
    74    has been changed since the last time the display has been updated,
    75    and to make these changes visible.  Preferably it would do that in
    76    a moderately intelligent way, i.e. fast.
    77 
    78    At its highest level, redisplay can be divided into 3 distinct
    79    steps, all of which are visible in `redisplay_internal':
    80 
    81     . decide which frames need their windows to be considered for redisplay
    82     . for each window whose display might need to be updated, compute
    83       a structure, called "glyph matrix", which describes how it
    84       should look on display
    85     . actually update the display of windows on the glass where the
    86       newly obtained glyph matrix differs from the one produced by the
    87       previous redisplay cycle
    88 
    89    The first of these steps is done by `redisplay_internal' itself, by
    90    looping through all the frames and testing their various flags,
    91    such as their visibility.  The result of this could be that only
    92    the selected window on the selected frame must be redisplayed, or
    93    it could conclude that other windows need to be considered as well.
    94 
    95    The second step considers each window that might need to be
    96    redisplayed.  This could be only the selected window, or the window
    97    trees of one or more frames.  The function which considers a window
    98    and decides whether it actually needs redisplay is
    99    `redisplay_window'.  It does so by looking at the changes in
   100    position of point, in buffer text, in text properties, overlays,
   101    etc.  These changes can be deduced from window and buffer
   102    structures, and from some global variables like `beg_unchanged' and
   103    `end_unchanged'.  The current contents of the display are recorded
   104    in a `glyph matrix', a two-dimensional matrix of glyph structures.
   105    Each row in such a matrix corresponds to a line on the display, and
   106    each glyph in a row corresponds to a column displaying a character,
   107    an image, or what else.  This matrix is called the `current glyph
   108    matrix', or `current matrix', in redisplay terminology.
   109 
   110    For buffer parts that have been changed since the last redisplay,
   111    `redisplay_window' constructs a second glyph matrix, the so called
   112    `desired glyph matrix' or short `desired matrix'.  It does so in
   113    the most optimal way possible, avoiding the examination of text
   114    that didn't change, reusing portions of the current matrix if
   115    possible, etc.  It could, in particular, decide that a window
   116    doesn't need to be redisplayed at all.
   117 
   118    This second step of redisplay also updates the parts of the desired
   119    matrix that correspond to the mode lines, header lines, and
   120    tab-lines of the windows which need that; see `display_mode_lines'.
   121 
   122    In the third and last step, the current and desired matrix are then
   123    compared to find a cheap way to update the display, e.g. by reusing
   124    part of the display by scrolling lines.  The actual update of the
   125    display of each window, by comparing the desired and the current
   126    matrix, is done by `update_window', which calls functions which
   127    draw to the glass (those functions are specific to the type of the
   128    window's frame: X, w32, NS, etc.).
   129 
   130    Once the display of a window on the glass has been updated, its
   131    desired matrix is used to update the corresponding rows of the
   132    current matrix, and then the desired matrix is discarded.
   133 
   134    You will find a lot of redisplay optimizations when you start
   135    looking at the innards of `redisplay_window'.  The overall goal of
   136    all these optimizations is to make redisplay fast because it is
   137    done frequently.  Some of these optimizations are implemented by
   138    the following functions:
   139 
   140     . try_cursor_movement
   141 
   142       This optimization is applicable if the text in the window did
   143       not change and did not scroll, only point moved, and it did not
   144       move off the displayed portion of the text.  In that case, the
   145       window's glyph matrix is still valid, and only the position of
   146       the cursor might need to be updated.
   147 
   148     . try_window_reusing_current_matrix
   149 
   150       This function reuses the current glyph matrix of a window when
   151       text has not changed, but the window start changed (e.g., due to
   152       scrolling).
   153 
   154     . try_window_id
   155 
   156       This function attempts to update a window's glyph matrix by
   157       reusing parts of its current glyph matrix.  It finds and reuses
   158       the part that was not changed, and regenerates the rest.  (The
   159       "id" part in the function's name stands for "insert/delete", not
   160       for "identification" or somesuch.)
   161 
   162     . try_window
   163 
   164       This function performs the full, unoptimized, generation of a
   165       single window's glyph matrix, assuming that its fonts were not
   166       changed and that the cursor will not end up in the scroll
   167       margins.  (Loading fonts requires re-adjustment of dimensions of
   168       glyph matrices, which makes this method impossible to use.)
   169 
   170    The optimizations are tried in sequence (some can be skipped if
   171    it is known that they are not applicable).  If none of the
   172    optimizations were successful, redisplay calls redisplay_windows,
   173    which performs a full redisplay of all windows.
   174 
   175    Note that there's one more important optimization up Emacs's
   176    sleeve, but it is related to actually redrawing the potentially
   177    changed portions of the window/frame as part of the third step, not
   178    to generating the desired matrices of those potentially changed
   179    portions.  Namely, the function `update_frame' and its subroutines,
   180    which you will find in dispnew.c, compare the desired matrices with
   181    the current matrices, and only redraw the portions that changed.
   182    So it could happen that the functions in this file for some reason
   183    decide that the entire desired matrix needs to be regenerated from
   184    scratch, and still only parts of the Emacs display, or even nothing
   185    at all, will be actually delivered to the glass, because
   186    `update_frame' has found that the new and the old screen contents
   187    are similar or identical.
   188 
   189    Desired matrices.
   190 
   191    Desired matrices are always built per Emacs window.  It is
   192    important to know that a desired matrix is in general "sparse": it
   193    only has some of the glyph rows "enabled".  This is because
   194    redisplay tries to optimize its work, and thus only generates
   195    glyphs for rows that need to be updated on the screen.  Rows that
   196    don't need to be updated are left "disabled", and their contents
   197    in the desired matrix should be ignored.
   198 
   199    The function `display_line' is the central function to look at if
   200    you are interested in how the rows of the desired matrix are
   201    produced.  It constructs one row in a desired matrix given an
   202    iterator structure containing both a buffer position and a
   203    description of the environment in which the text is to be
   204    displayed.  But this is too early, read on.
   205 
   206    Glyph rows.
   207 
   208    A glyph row is an array of `struct glyph', where each glyph element
   209    describes a "display element" to be shown on the screen.  More
   210    accurately, a glyph row can have up to 3 different arrays of
   211    glyphs: one each for every display margins, and one for the "text
   212    area", where buffer text is displayed.  The text-area glyph array
   213    is always present, whereas the arrays for the marginal areas are
   214    present (non-empty) only if the corresponding display margin is
   215    shown in the window.  If the glyph array for a marginal area is not
   216    present its beginning and end coincide, i.e. such arrays are
   217    actually empty (they contain no glyphs).  Frame glyph matrices, used
   218    on text-mode terminals (see below) never have marginal areas; they
   219    treat the entire frame-wide row of glyphs as a single large "text
   220    area".
   221 
   222    Iteration over buffer and strings.
   223 
   224    Characters and pixmaps displayed for a range of buffer text depend
   225    on various settings of buffers and windows, on overlays and text
   226    properties, on display tables, on selective display.  The good news
   227    is that all this hairy stuff is hidden behind a small set of
   228    interface functions taking an iterator structure (`struct it')
   229    argument.
   230 
   231    Iteration over things to be displayed is then simple.  It is
   232    started by initializing an iterator with a call to `init_iterator',
   233    passing it the buffer position where to start iteration.  For
   234    iteration over strings, pass -1 as the position to `init_iterator',
   235    and call `reseat_to_string' when the string is ready, to initialize
   236    the iterator for that string.  Thereafter, calls to
   237    `get_next_display_element' fill the iterator structure with
   238    relevant information about the next thing to display.  Calls to
   239    `set_iterator_to_next' move the iterator to the next thing.
   240 
   241    Besides this, an iterator also contains information about the
   242    display environment in which glyphs for display elements are to be
   243    produced.  It has fields for the width and height of the display,
   244    the information whether long lines are truncated or continued, a
   245    current X and Y position, the face currently in effect, and lots of
   246    other stuff you can better see in dispextern.h.
   247 
   248    The "stop position".
   249 
   250    Some of the fields maintained by the iterator change relatively
   251    infrequently.  These include the face of the characters, whether
   252    text is invisible, the object (buffer or display or overlay string)
   253    being iterated, character composition info, etc.  For any given
   254    buffer or string position, the sources of information that affects
   255    the display can be determined by calling the appropriate
   256    primitives, such as `Fnext_single_property_change', but both these
   257    calls and the processing of their return values is relatively
   258    expensive.  To optimize redisplay, the display engine checks these
   259    sources of display information only when needed, not for every
   260    character.  To that end, it always maintains the position of the
   261    next place where it must stop and re-examine all those potential
   262    sources.  This is called "the stop position" and is stored in the
   263    `stop_charpos' field of the iterator.  The stop position is updated
   264    by `compute_stop_pos', which is called whenever the iteration
   265    reaches the current stop position and processes it.  Processing a
   266    stop position is done by `handle_stop', which invokes a series of
   267    handlers, one each for every potential source of display-related
   268    information; see the `it_props' array for those handlers.  For
   269    example, one handler is `handle_face_prop', which detects changes
   270    in face properties, and supplies the face ID that the iterator will
   271    use for all the glyphs it generates up to the next stop position;
   272    this face ID is the result of "realizing" the face specified by the
   273    relevant text properties at this position (see xfaces.c).  Each
   274    handler called by `handle_stop' processes the sources of display
   275    information for which it is "responsible", and returns a value
   276    which tells `handle_stop' what to do next.
   277 
   278    Once `handle_stop' returns, the information it stores in the
   279    iterator fields will not be refreshed until the iteration reaches
   280    the next stop position, which is computed by `compute_stop_pos'
   281    called at the end of `handle_stop'.  `compute_stop_pos' examines
   282    the buffer's or string's interval tree to determine where the text
   283    properties change, finds the next position where overlays and
   284    character composition can change, and stores in `stop_charpos' the
   285    closest position where any of these factors should be reconsidered.
   286 
   287    Handling of the stop position is done as part of the code in
   288    `get_next_display_element'.
   289 
   290    Producing glyphs.
   291 
   292    Glyphs in a desired matrix are normally constructed in a loop
   293    calling `get_next_display_element' and then `PRODUCE_GLYPHS'.  The
   294    call to `PRODUCE_GLYPHS' will fill the iterator structure with
   295    pixel information about the element being displayed and at the same
   296    time will produce glyphs for it.  If the display element fits on
   297    the line being displayed, `set_iterator_to_next' is called next,
   298    otherwise the glyphs produced are discarded, and `display_line'
   299    marks this glyph row as a "continued line".  The function
   300    `display_line' is the workhorse of filling glyph rows in the
   301    desired matrix with glyphs.  In addition to producing glyphs, it
   302    also handles line truncation and continuation, word wrap, and
   303    cursor positioning (for the latter, see `set_cursor_from_row').
   304 
   305    Frame matrices.
   306 
   307    That just couldn't be all, could it?  What about terminal types not
   308    supporting operations on sub-windows of the screen (a.k.a. "TTY" or
   309    "text-mode terminals")?  To update the display on such a terminal,
   310    window-based glyph matrices are not well suited.  To be able to
   311    reuse part of the display (scrolling lines up and down), we must
   312    instead have a view of the whole screen.  This is what `frame
   313    matrices' are for.  They are a trick.
   314 
   315    Frames on text terminals have a glyph pool.  Windows on such a
   316    frame sub-allocate their glyph memory from their frame's glyph
   317    pool.  The frame itself is given its own glyph matrices.  By
   318    coincidence---or maybe something else---rows in window glyph
   319    matrices are slices of corresponding rows in frame matrices.  Thus
   320    writing to window matrices implicitly updates a frame matrix which
   321    provides us with the view of the whole screen that we originally
   322    wanted to have without having to move many bytes around.  Then
   323    updating all the visible windows on text-terminal frames is done by
   324    using the frame matrices, which allows frame-global optimization of
   325    what is actually written to the glass.
   326 
   327    Frame matrices don't have marginal areas, only a text area.  That
   328    is, the entire row of glyphs that spans the width of a text-mode
   329    frame is treated as a single large "text area" for the purposes of
   330    manipulating and updating a frame glyph matrix.
   331 
   332    To be honest, there is a little bit more done for frame matrices,
   333    but not much more.  If you plan to extend that code, take a look at
   334    dispnew.c.  The function build_frame_matrix is a good starting
   335    point.
   336 
   337    Simulating display.
   338 
   339    Some of Emacs commands and functions need to take display layout
   340    into consideration.  For example, C-n moves to the next screen
   341    line, but to implement that, Emacs needs to find the buffer
   342    position which is directly below the cursor position on display.
   343    This is not trivial when buffer display includes variable-size
   344    elements such as different fonts, tall images, etc.
   345 
   346    To solve this problem, the display engine implements several
   347    functions that can move through buffer text in the same manner as
   348    `display_line' and `display_string' do, but without producing any
   349    glyphs for the glyph matrices.  The workhorse of this is
   350    `move_it_in_display_line_to'.  Its code and logic are very similar
   351    to `display_line', but it differs in two important aspects: it
   352    doesn't produce glyphs for any glyph matrix, and it returns a
   353    status telling the caller how it ended the iteration: whether it
   354    reached the required position, hit the end of line, arrived at the
   355    window edge without exhausting the buffer's line, etc.  Since the
   356    glyphs are not produced, the layout information available to the
   357    callers of this function is what is recorded in `struct it' by the
   358    iteration process.
   359 
   360    Several higher-level functions call `move_it_in_display_line_to' to
   361    perform more complex tasks: `move_it_by_lines' can move N lines up
   362    or down from a given buffer position and `move_it_to' can move to a
   363    given buffer position or to a given X or Y pixel coordinate.
   364 
   365    These functions are called by the display engine itself as well,
   366    when it needs to make layout decisions before producing the glyphs.
   367    For example, one of the first things to decide when redisplaying a
   368    window is where to put the `window-start' position; if the window
   369    is to be recentered (the default), Emacs makes that decision by
   370    starting from the position of point, then moving up the number of
   371    lines corresponding to half the window height using
   372    `move_it_by_lines'.
   373 
   374    Bidirectional display.
   375 
   376    Bidirectional display adds quite some hair to this already complex
   377    design.  The good news are that a large portion of that hairy stuff
   378    is hidden in bidi.c behind only 3 interfaces.  bidi.c implements a
   379    reordering engine which is called by `set_iterator_to_next' and
   380    returns the next character to display in the visual order.  See
   381    commentary on bidi.c for more details.  As far as redisplay is
   382    concerned, the effect of calling `bidi_move_to_visually_next', the
   383    main interface of the reordering engine, is that the iterator gets
   384    magically placed on the buffer or string position that is to be
   385    displayed next in the visual order.  In other words, a linear
   386    iteration through the buffer/string is replaced with a non-linear
   387    one.  All the rest of the redisplay is oblivious to the bidi
   388    reordering.
   389 
   390    Well, almost oblivious---there are still complications, most of
   391    them due to the fact that buffer and string positions no longer
   392    change monotonously with glyph indices in a glyph row.  Moreover,
   393    for continued lines, the buffer positions may not even be
   394    monotonously changing with vertical positions.  Also, accounting
   395    for face changes, overlays, etc. becomes more complex because
   396    non-linear iteration could potentially skip many positions with
   397    such changes, and then cross them again on the way back (see
   398    `handle_stop_backwards')...
   399 
   400    One other prominent effect of bidirectional display is that some
   401    paragraphs of text need to be displayed starting at the right
   402    margin of the window---the so-called right-to-left, or R2L
   403    paragraphs.  R2L paragraphs are displayed with R2L glyph rows,
   404    which have their `reversed_p' flag set.  The bidi reordering engine
   405    produces characters in such rows starting from the character which
   406    should be the rightmost on display.  `PRODUCE_GLYPHS' then reverses
   407    the order, when it fills up the glyph row whose `reversed_p' flag
   408    is set, by prepending each new glyph to what is already there,
   409    instead of appending it.  When the glyph row is complete, the
   410    function `extend_face_to_end_of_line' fills the empty space to the
   411    left of the leftmost character with special glyphs, which will
   412    display as, well, empty.  On text terminals, these special glyphs
   413    are simply blank characters.  On graphics terminals, there's a
   414    single stretch glyph of a suitably computed width.  Both the blanks
   415    and the stretch glyph are given the face of the background of the
   416    line.  This way, the terminal-specific back-end can still draw the
   417    glyphs left to right, even for R2L lines.
   418 
   419    Bidirectional display and character compositions.
   420 
   421    Some scripts cannot be displayed by drawing each character
   422    individually, because adjacent characters change each other's shape
   423    on display.  For example, Arabic and Indic scripts belong to this
   424    category.
   425 
   426    Emacs display supports this by providing "character compositions",
   427    most of which is implemented in composite.c.  During the buffer
   428    scan that delivers characters to `PRODUCE_GLYPHS', if the next
   429    character to be delivered is a composed character, the iteration
   430    calls `composition_reseat_it' and `next_element_from_composition'.
   431    If they succeed to compose the character with one or more of the
   432    following characters, the whole sequence of characters that were
   433    composed is recorded in the `struct composition_it' object that is
   434    part of the buffer iterator.  The composed sequence could produce
   435    one or more font glyphs (called "grapheme clusters") on the screen.
   436    Each of these grapheme clusters is then delivered to
   437    `PRODUCE_GLYPHS' in the direction corresponding to the current bidi
   438    scan direction (recorded in the `scan_dir' member of the `struct
   439    bidi_it' object that is part of the iterator).  In particular, if
   440    the bidi iterator currently scans the buffer backwards, the
   441    grapheme clusters are delivered back to front.  This reorders the
   442    grapheme clusters as appropriate for the current bidi context.
   443    Note that this means that the grapheme clusters are always stored
   444    in the `LGSTRING' object (see composite.c) in the logical order.
   445 
   446    Moving an iterator in bidirectional text
   447    without producing glyphs.
   448 
   449    Note one important detail mentioned above: that the bidi reordering
   450    engine, driven by the iterator, produces characters in R2L rows
   451    starting at the character that will be the rightmost on display.
   452    As far as the iterator is concerned, the geometry of such rows is
   453    still left to right, i.e. the iterator "thinks" the first character
   454    is at the leftmost pixel position.  The iterator does not know that
   455    `PRODUCE_GLYPHS' reverses the order of the glyphs that the iterator
   456    delivers.  This is important when functions from the `move_it_*'
   457    family are used to get to certain screen position or to match
   458    screen coordinates with buffer coordinates: these functions use the
   459    iterator geometry, which is left to right even in R2L paragraphs.
   460    This works well with most callers of `move_it_*', because they need
   461    to get to a specific column, and columns are still numbered in the
   462    reading order, i.e. the rightmost character in a R2L paragraph is
   463    still column zero.  But some callers do not get well with this; a
   464    notable example is mouse clicks that need to find the character
   465    that corresponds to certain pixel coordinates.  See
   466    `buffer_posn_from_coords' in dispnew.c for how this is handled.  */
   467 
   468 #include <config.h>
   469 #include <stdlib.h>
   470 #include <limits.h>
   471 #include <math.h>
   472 
   473 #include "lisp.h"
   474 #include "atimer.h"
   475 #include "composite.h"
   476 #include "keyboard.h"
   477 #include "sysstdio.h"
   478 #include "systime.h"
   479 #include "frame.h"
   480 #include "window.h"
   481 #include "termchar.h"
   482 #include "dispextern.h"
   483 #include "character.h"
   484 #include "category.h"
   485 #include "buffer.h"
   486 #include "charset.h"
   487 #include "indent.h"
   488 #include "commands.h"
   489 #include "keymap.h"
   490 #include "disptab.h"
   491 #include "termhooks.h"
   492 #include "termopts.h"
   493 #include "intervals.h"
   494 #include "coding.h"
   495 #include "region-cache.h"
   496 #include "font.h"
   497 #include "fontset.h"
   498 #include "blockinput.h"
   499 #include "xwidget.h"
   500 #ifdef HAVE_WINDOW_SYSTEM
   501 #include TERM_HEADER
   502 #endif /* HAVE_WINDOW_SYSTEM */
   503 
   504 #ifndef FRAME_OUTPUT_DATA
   505 #define FRAME_OUTPUT_DATA(f) (NULL)
   506 #endif
   507 
   508 #define DISP_INFINITY 10000000
   509 
   510 /* Holds the list (error).  */
   511 static Lisp_Object list_of_error;
   512 
   513 #ifdef HAVE_WINDOW_SYSTEM
   514 
   515 /* Test if overflow newline into fringe.  Called with iterator IT
   516    at or past right window margin, and with IT->current_x set.  */
   517 
   518 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT)             \
   519   (!NILP (Voverflow_newline_into_fringe)                \
   520    && FRAME_WINDOW_P ((IT)->f)                          \
   521    && ((IT)->bidi_it.paragraph_dir == R2L               \
   522        ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0)       \
   523        : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0))     \
   524    && (IT)->current_x == (IT)->last_visible_x)
   525 
   526 #else /* !HAVE_WINDOW_SYSTEM */
   527 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) false
   528 #endif /* HAVE_WINDOW_SYSTEM */
   529 
   530 /* Test if the display element loaded in IT, or the underlying buffer
   531    or string character, is a space or a TAB character.  This is used
   532    to determine where word wrapping can occur.  */
   533 
   534 #define IT_DISPLAYING_WHITESPACE(it)                                    \
   535   ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))        \
   536    || ((STRINGP (it->string)                                            \
   537         && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' '           \
   538             || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t'))     \
   539        || (it->s                                                        \
   540            && (it->s[IT_BYTEPOS (*it)] == ' '                           \
   541                || it->s[IT_BYTEPOS (*it)] == '\t'))                     \
   542        || (IT_BYTEPOS (*it) < ZV_BYTE                                   \
   543            && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' '                 \
   544                || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t'))))
   545 
   546 /* These are the category sets we use.  They are defined by
   547    kinsoku.el and characters.el.  */
   548 #define NOT_AT_EOL '<'
   549 #define NOT_AT_BOL '>'
   550 #define LINE_BREAKABLE '|'
   551 
   552 static bool
   553 it_char_has_category(struct it *it, int cat)
   554 {
   555   int ch = 0;
   556   if (it->what == IT_CHARACTER)
   557     ch = it->c;
   558   else if (STRINGP (it->string))
   559     ch = SREF (it->string, IT_STRING_BYTEPOS (*it));
   560   else if (it->s)
   561     ch = it->s[IT_BYTEPOS (*it)];
   562   else if (IT_BYTEPOS (*it) < ZV_BYTE)
   563     ch = *BYTE_POS_ADDR (IT_BYTEPOS (*it));
   564 
   565   if (ch == 0)
   566     return false;
   567   else
   568     return CHAR_HAS_CATEGORY (ch, cat);
   569 }
   570 
   571 /* Return true if the current character allows wrapping before it.   */
   572 static bool
   573 char_can_wrap_before (struct it *it)
   574 {
   575   if (!word_wrap_by_category)
   576     return !IT_DISPLAYING_WHITESPACE (it);
   577 
   578   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   579      Because in RTL paragraph, each glyph is prepended to the last
   580      one, effectively drawing right to left.  */
   581   int not_at_bol;
   582   if (it->glyph_row && it->glyph_row->reversed_p)
   583     not_at_bol = NOT_AT_EOL;
   584   else
   585     not_at_bol = NOT_AT_BOL;
   586   /* You cannot wrap before a space or tab because that way you'll
   587      have space and tab at the beginning of next line.  */
   588   return (!IT_DISPLAYING_WHITESPACE (it)
   589           /* Can be at BOL.  */
   590           && !it_char_has_category (it, not_at_bol));
   591 }
   592 
   593 /* Return true if the current character allows wrapping after it.   */
   594 static bool
   595 char_can_wrap_after (struct it *it)
   596 {
   597   if (!word_wrap_by_category)
   598     return IT_DISPLAYING_WHITESPACE (it);
   599 
   600   /* For CJK (LTR) text in RTL paragraph, EOL and BOL are flipped.
   601      Because in RTL paragraph, each glyph is prepended to the last
   602      one, effectively drawing right to left.  */
   603   int not_at_eol;
   604   if (it->glyph_row && it->glyph_row->reversed_p)
   605     not_at_eol = NOT_AT_BOL;
   606   else
   607     not_at_eol = NOT_AT_EOL;
   608 
   609   return (IT_DISPLAYING_WHITESPACE (it)
   610           /* Can break after && can be at EOL.  */
   611           || (it_char_has_category (it, LINE_BREAKABLE)
   612               && !it_char_has_category (it, not_at_eol)));
   613 }
   614 
   615 #undef IT_DISPLAYING_WHITESPACE
   616 #undef NOT_AT_EOL
   617 #undef NOT_AT_BOL
   618 #undef LINE_BREAKABLE
   619 
   620 /* If all the conditions needed to print the fill column indicator are
   621    met, return the (nonnegative) column number, else return a negative
   622    value.  */
   623 static int
   624 fill_column_indicator_column (struct it *it, int char_width)
   625 {
   626   if (display_fill_column_indicator
   627       && !it->w->pseudo_window_p
   628       && it->continuation_lines_width == 0
   629       && CHARACTERP (Vdisplay_fill_column_indicator_character))
   630     {
   631       Lisp_Object col = (EQ (Vdisplay_fill_column_indicator_column, Qt)
   632                          ? BVAR (current_buffer, fill_column)
   633                          : Vdisplay_fill_column_indicator_column);
   634 
   635       /* The stretch width needs to consider the latter
   636          added glyph in append_space_for_newline.  */
   637       if (RANGED_FIXNUMP (0, col, INT_MAX))
   638         {
   639           int icol = XFIXNUM (col);
   640           if (!ckd_mul (&icol, icol, char_width)
   641               && !ckd_add (&icol, icol, it->lnum_pixel_width))
   642             return icol;
   643         }
   644     }
   645   return -1;
   646 }
   647 
   648 /* True means print newline to stdout before next mini-buffer message.  */
   649 
   650 bool noninteractive_need_newline;
   651 
   652 /* True means print newline to message log before next message.  */
   653 
   654 static bool message_log_need_newline;
   655 
   656 /* Three markers that message_dolog uses.
   657    It could allocate them itself, but that causes trouble
   658    in handling memory-full errors.  */
   659 static Lisp_Object message_dolog_marker1;
   660 static Lisp_Object message_dolog_marker2;
   661 static Lisp_Object message_dolog_marker3;
   662 
   663 /* The buffer position of the first character appearing entirely or
   664    partially on the line of the selected window which contains the
   665    cursor; <= 0 if not known.  Set by set_cursor_from_row, used for
   666    redisplay optimization in redisplay_internal.  */
   667 
   668 static struct text_pos this_line_start_pos;
   669 
   670 /* Number of characters past the end of the line above, including the
   671    terminating newline.  */
   672 
   673 static struct text_pos this_line_end_pos;
   674 
   675 /* The vertical positions and the height of this line.  */
   676 
   677 static int this_line_vpos;
   678 static int this_line_y;
   679 static int this_line_pixel_height;
   680 
   681 /* X position at which this display line starts.  Usually zero;
   682    negative if first character is partially visible.  */
   683 
   684 static int this_line_start_x;
   685 
   686 /* The smallest character position seen by move_it_* functions as they
   687    move across display lines.  Used to set MATRIX_ROW_START_CHARPOS of
   688    hscrolled lines, see display_line.  */
   689 
   690 static struct text_pos this_line_min_pos;
   691 
   692 /* Buffer that this_line_.* variables are referring to.  */
   693 
   694 static struct buffer *this_line_buffer;
   695 
   696 /* True if an overlay arrow has been displayed in this window.  */
   697 
   698 static bool overlay_arrow_seen;
   699 
   700 /* Vector containing glyphs for an ellipsis `...'.  */
   701 
   702 static Lisp_Object default_invis_vector[3];
   703 
   704 /* This is the window where the echo area message was displayed.  It
   705    is always a mini-buffer window, but it may not be the same window
   706    currently active as a mini-buffer.  */
   707 
   708 Lisp_Object echo_area_window;
   709 
   710 /* Stack of messages, which are pushed by push_message and popped and
   711    displayed by restore_message.  */
   712 
   713 static Lisp_Object Vmessage_stack;
   714 
   715 /* True means multibyte characters were enabled when the echo area
   716    message was specified.  */
   717 
   718 static bool message_enable_multibyte;
   719 
   720 /* At each redisplay cycle, we should refresh everything there is to refresh.
   721    To do that efficiently, we use many optimizations that try to make sure we
   722    don't waste too much time updating things that haven't changed.
   723    The coarsest such optimization is that, in the most common cases, we only
   724    look at the selected-window.
   725 
   726    To know whether other windows should be considered for redisplay, we use the
   727    variable windows_or_buffers_changed: as long as it is 0, it means that we
   728    have not noticed anything that should require updating anything else than
   729    the selected-window.  If it is set to REDISPLAY_SOME, it means that since
   730    last redisplay, some changes have been made which could impact other
   731    windows.  To know which ones need redisplay, every buffer, window, and frame
   732    has a `redisplay' bit, which (if true) means that this object needs to be
   733    redisplayed.  If windows_or_buffers_changed is 0, we know there's no point
   734    looking for those `redisplay' bits (actually, there might be some such bits
   735    set, but then only on objects which aren't displayed anyway).
   736 
   737    OTOH if it's non-zero we will have to loop through all windows and then
   738    check the `redisplay' bit of the corresponding window, frame, and buffer, in
   739    order to decide whether that window needs attention or not.  Note that we
   740    can't just look at the frame's redisplay bit to decide that the whole frame
   741    can be skipped, since even if the frame's redisplay bit is unset, some of
   742    its windows's redisplay bits may be set.
   743 
   744    Mostly for historical reasons, windows_or_buffers_changed can also take
   745    other non-zero values.  In that case, the precise value doesn't matter (it
   746    encodes the cause of the setting but is only used for debugging purposes),
   747    and what it means is that we shouldn't pay attention to any `redisplay' bits
   748    and we should simply try and redisplay every window out there.  */
   749 
   750 int windows_or_buffers_changed;
   751 
   752 /* Nonzero if we should redraw the mode lines on the next redisplay.
   753    Similarly to `windows_or_buffers_changed', if it has value REDISPLAY_SOME,
   754    then only redisplay the mode lines in those buffers/windows/frames where the
   755    `redisplay' bit has been set.
   756    For any other value, redisplay all mode lines (the number used is then only
   757    used to track down the cause for this full-redisplay).
   758 
   759    Since the frame title uses the same %-constructs as the mode line
   760    (except %c, %C, and %l), if this variable is non-zero, we also consider
   761    redisplaying the title of each frame, see gui_consider_frame_title.
   762 
   763    The `redisplay' bits are the same as those used for
   764    windows_or_buffers_changed, and setting windows_or_buffers_changed also
   765    causes recomputation of the mode lines of all those windows.  IOW this
   766    variable only has an effect if windows_or_buffers_changed is zero, in which
   767    case we should only need to redisplay the mode-line of those objects with
   768    a `redisplay' bit set but not the window's text content (tho we may still
   769    need to refresh the text content of the selected-window).  */
   770 
   771 int update_mode_lines;
   772 
   773 /* True after display_mode_line if %l was used and it displayed a
   774    line number.  */
   775 
   776 static bool line_number_displayed;
   777 
   778 /* Current, index 0, and last displayed echo area message.  Either
   779    buffers from echo_buffers, or nil to indicate no message.  */
   780 
   781 Lisp_Object echo_area_buffer[2];
   782 
   783 /* The buffers referenced from echo_area_buffer.  */
   784 
   785 static Lisp_Object echo_buffer[2];
   786 
   787 /* A vector saved used in with_area_buffer to reduce consing.  */
   788 
   789 static Lisp_Object Vwith_echo_area_save_vector;
   790 
   791 /* True means display_echo_area should display the last echo area
   792    message again.  Set by redisplay_preserve_echo_area.  */
   793 
   794 static bool display_last_displayed_message_p;
   795 
   796 /* True if echo area is being used by print; false if being used by
   797    message.  */
   798 
   799 static bool message_buf_print;
   800 
   801 /* Set to true in clear_message to make redisplay_internal aware
   802    of an emptied echo area.  */
   803 
   804 static bool message_cleared_p;
   805 
   806 /* A scratch glyph row with contents used for generating truncation
   807    glyphs and overlay-arrow glyphs.  */
   808 
   809 #define MAX_SCRATCH_GLYPHS 100
   810 static struct glyph_row scratch_glyph_row;
   811 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
   812 
   813 /* Ascent and height of the last line processed by move_it_to.  */
   814 
   815 static int last_height;
   816 
   817 /* True if there's a help-echo in the echo area.  */
   818 
   819 bool help_echo_showing_p;
   820 
   821 /* The maximum distance to look ahead for text properties.  Values
   822    that are too small let us call compute_char_face and similar
   823    functions too often which is expensive.  Values that are too large
   824    let us call compute_char_face and alike too often because we
   825    might not be interested in text properties that far away.  */
   826 
   827 #define TEXT_PROP_DISTANCE_LIMIT 100
   828 
   829 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
   830    iterator state and later restore it.  This is needed because the
   831    bidi iterator on bidi.c keeps a stacked cache of its states, which
   832    is really a singleton.  When we use scratch iterator objects to
   833    move around the buffer, we can cause the bidi cache to be pushed or
   834    popped, and therefore we need to restore the cache state when we
   835    return to the original iterator.  */
   836 #define SAVE_IT(ITCOPY, ITORIG, CACHE)          \
   837   do {                                          \
   838     if (CACHE)                                  \
   839       bidi_unshelve_cache (CACHE, true);        \
   840     ITCOPY = ITORIG;                            \
   841     CACHE = bidi_shelve_cache ();               \
   842   } while (false)
   843 
   844 #define RESTORE_IT(pITORIG, pITCOPY, CACHE)     \
   845   do {                                          \
   846     if (pITORIG != pITCOPY)                     \
   847       *(pITORIG) = *(pITCOPY);                  \
   848     bidi_unshelve_cache (CACHE, false);         \
   849     CACHE = NULL;                               \
   850   } while (false)
   851 
   852 /* Functions to mark elements as needing redisplay.  */
   853 enum { REDISPLAY_SOME = 2};     /* Arbitrary choice.  */
   854 
   855 static bool calc_pixel_width_or_height (double *, struct it *, Lisp_Object,
   856                                         struct font *, bool, int *);
   857 
   858 void
   859 redisplay_other_windows (void)
   860 {
   861   if (!windows_or_buffers_changed)
   862     windows_or_buffers_changed = REDISPLAY_SOME;
   863 }
   864 
   865 void
   866 wset_redisplay (struct window *w)
   867 {
   868   /* Beware: selected_window can be nil during early stages.  */
   869   if (!BASE_EQ (make_lisp_ptr (w, Lisp_Vectorlike), selected_window))
   870     redisplay_other_windows ();
   871   w->redisplay = true;
   872 }
   873 
   874 void
   875 fset_redisplay (struct frame *f)
   876 {
   877   redisplay_other_windows ();
   878   f->redisplay = true;
   879 }
   880 
   881 void
   882 bset_redisplay (struct buffer *b)
   883 {
   884   int count = buffer_window_count (b);
   885   if (count > 0)
   886     {
   887       /* ... it's visible in other window than selected,  */
   888       if (count > 1 || b != XBUFFER (XWINDOW (selected_window)->contents))
   889         redisplay_other_windows ();
   890       /* Even if we don't set windows_or_buffers_changed, do set `redisplay'
   891          so that if we later set windows_or_buffers_changed, this buffer will
   892          not be omitted.  */
   893       b->text->redisplay = true;
   894     }
   895 }
   896 
   897 void
   898 bset_update_mode_line (struct buffer *b)
   899 {
   900   if (!update_mode_lines)
   901     update_mode_lines = REDISPLAY_SOME;
   902   b->text->redisplay = true;
   903 }
   904 
   905 void
   906 wset_update_mode_line (struct window *w)
   907 {
   908   w->update_mode_line = true;
   909   /* When a window's mode line needs to be updated, the window's frame's
   910      title may also need to be updated, but we don't need to worry about it
   911      here.  Instead, `gui_consider_frame_title' is automatically called
   912      whenever w->update_mode_line is set for that frame's selected window.
   913      But for this to work reliably, we have to make sure the window
   914      is considered, so we have to mark it for redisplay.  */
   915   wset_redisplay (w);
   916 }
   917 
   918 DEFUN ("set-buffer-redisplay", Fset_buffer_redisplay,
   919        Sset_buffer_redisplay, 4, 4, 0,
   920        doc: /* Mark the current buffer for redisplay.
   921 This function may be passed to `add-variable-watcher'.  */)
   922   (Lisp_Object symbol, Lisp_Object newval, Lisp_Object op, Lisp_Object where)
   923 {
   924   bset_update_mode_line (current_buffer);
   925   current_buffer->prevent_redisplay_optimizations_p = true;
   926   return Qnil;
   927 }
   928 
   929 /* redisplay_trace is for displaying traces of redisplay.
   930    If Emacs was compiled with GLYPH_DEBUG defined, the variable
   931    trace_redisplay_p can be set to a non-zero value in debugging
   932    sessions to activate traces.  */
   933 #ifdef GLYPH_DEBUG
   934 extern bool trace_redisplay_p EXTERNALLY_VISIBLE;
   935 bool trace_redisplay_p;
   936 #else
   937 enum { trace_redisplay_p = false };
   938 #endif
   939 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   940 redisplay_trace (char const *fmt, ...)
   941 {
   942   if (trace_redisplay_p)
   943     {
   944       va_list ap;
   945       va_start (ap, fmt);
   946       vprintf (fmt, ap);
   947       va_end (ap);
   948     }
   949 }
   950 
   951 #ifdef DEBUG_TRACE_MOVE
   952 extern bool trace_move EXTERNALLY_VISIBLE;
   953 bool trace_move;
   954 #else
   955 enum { trace_move = false };
   956 #endif
   957 static void ATTRIBUTE_FORMAT_PRINTF (1, 2)
   958 move_trace (char const *fmt, ...)
   959 {
   960   if (trace_move)
   961     {
   962       va_list ap;
   963       va_start (ap, fmt);
   964       vprintf (fmt, ap);
   965       va_end (ap);
   966     }
   967 }
   968 
   969 /* Buffer being redisplayed -- for redisplay_window_error.  */
   970 
   971 static struct buffer *displayed_buffer;
   972 
   973 /* Value returned from text property handlers (see below).  */
   974 
   975 enum prop_handled
   976 {
   977   HANDLED_NORMALLY,
   978   HANDLED_RECOMPUTE_PROPS,
   979   HANDLED_OVERLAY_STRING_CONSUMED,
   980   HANDLED_RETURN
   981 };
   982 
   983 /* A description of text properties that redisplay is interested
   984    in.  */
   985 
   986 struct props
   987 {
   988   /* The symbol index of the name of the property.  */
   989   short name;
   990 
   991   /* A unique index for the property.  */
   992   enum prop_idx idx;
   993 
   994   /* A handler function called to set up iterator IT from the property
   995      at IT's current position.  Value is used to steer handle_stop.  */
   996   enum prop_handled (*handler) (struct it *it);
   997 };
   998 
   999 static enum prop_handled handle_face_prop (struct it *);
  1000 static enum prop_handled handle_invisible_prop (struct it *);
  1001 static enum prop_handled handle_display_prop (struct it *);
  1002 static enum prop_handled handle_composition_prop (struct it *);
  1003 static enum prop_handled handle_overlay_change (struct it *);
  1004 static enum prop_handled handle_fontified_prop (struct it *);
  1005 
  1006 /* Properties handled by iterators.  */
  1007 
  1008 static struct props it_props[] =
  1009 {
  1010   {SYMBOL_INDEX (Qfontified),   FONTIFIED_PROP_IDX,     handle_fontified_prop},
  1011   /* Handle `face' before `display' because some sub-properties of
  1012      `display' need to know the face.  */
  1013   {SYMBOL_INDEX (Qface),        FACE_PROP_IDX,          handle_face_prop},
  1014   {SYMBOL_INDEX (Qdisplay),     DISPLAY_PROP_IDX,       handle_display_prop},
  1015   {SYMBOL_INDEX (Qinvisible),   INVISIBLE_PROP_IDX,     handle_invisible_prop},
  1016   {SYMBOL_INDEX (Qcomposition), COMPOSITION_PROP_IDX, handle_composition_prop},
  1017   {0,                           0,                      NULL}
  1018 };
  1019 
  1020 /* Enumeration returned by some move_it_.* functions internally.  */
  1021 
  1022 enum move_it_result
  1023 {
  1024   /* Not used.  Undefined value.  */
  1025   MOVE_UNDEFINED,
  1026 
  1027   /* Move ended at the requested buffer position or ZV.  */
  1028   MOVE_POS_MATCH_OR_ZV,
  1029 
  1030   /* Move ended at the requested X pixel position.  */
  1031   MOVE_X_REACHED,
  1032 
  1033   /* Move within a line ended at the end of a line that must be
  1034      continued.  */
  1035   MOVE_LINE_CONTINUED,
  1036 
  1037   /* Move within a line ended at the end of a line that would
  1038      be displayed truncated.  */
  1039   MOVE_LINE_TRUNCATED,
  1040 
  1041   /* Move within a line ended at a line end.  */
  1042   MOVE_NEWLINE_OR_CR
  1043 };
  1044 
  1045 /* This counter is used to clear the face cache every once in a while
  1046    in redisplay_internal.  It is incremented for each redisplay.
  1047    Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
  1048    cleared.  */
  1049 
  1050 #define CLEAR_FACE_CACHE_COUNT  500
  1051 static int clear_face_cache_count;
  1052 
  1053 /* Similarly for the image cache.  */
  1054 
  1055 #ifdef HAVE_WINDOW_SYSTEM
  1056 #define CLEAR_IMAGE_CACHE_COUNT 101
  1057 static int clear_image_cache_count;
  1058 
  1059 /* Null glyph slice */
  1060 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
  1061 #endif
  1062 
  1063 /* True while redisplay_internal is in progress.  */
  1064 
  1065 bool redisplaying_p;
  1066 
  1067 /* True while some display-engine code is working on layout of some
  1068    window.
  1069 
  1070    WARNING: Use sparingly, preferably only in top level of commands
  1071    and important functions, because using it in nested calls might
  1072    reset the flag when the inner call returns, behind the back of
  1073    the callers.  */
  1074 bool display_working_on_window_p;
  1075 
  1076 /* If a string, XTread_socket generates an event to display that string.
  1077    (The display is done in read_char.)  */
  1078 
  1079 Lisp_Object help_echo_string;
  1080 Lisp_Object help_echo_window;
  1081 Lisp_Object help_echo_object;
  1082 ptrdiff_t help_echo_pos;
  1083 
  1084 /* Temporary variable for XTread_socket.  */
  1085 
  1086 Lisp_Object previous_help_echo_string;
  1087 
  1088 /* Platform-independent portion of hourglass implementation.  */
  1089 
  1090 #ifdef HAVE_WINDOW_SYSTEM
  1091 
  1092 /* True means an hourglass cursor is currently shown.  */
  1093 static bool hourglass_shown_p;
  1094 
  1095 /* If non-null, an asynchronous timer that, when it expires, displays
  1096    an hourglass cursor on all frames.  */
  1097 static struct atimer *hourglass_atimer;
  1098 
  1099 #endif /* HAVE_WINDOW_SYSTEM */
  1100 
  1101 /* Default number of seconds to wait before displaying an hourglass
  1102    cursor.  */
  1103 #define DEFAULT_HOURGLASS_DELAY 1
  1104 
  1105 #ifdef HAVE_WINDOW_SYSTEM
  1106 
  1107 /* Default pixel width of `thin-space' display method.  */
  1108 #define THIN_SPACE_WIDTH 1
  1109 
  1110 #endif /* HAVE_WINDOW_SYSTEM */
  1111 
  1112 /* Function prototypes.  */
  1113 
  1114 static void setup_for_ellipsis (struct it *, int);
  1115 static void set_iterator_to_next (struct it *, bool);
  1116 static void mark_window_display_accurate_1 (struct window *, bool);
  1117 static bool row_for_charpos_p (struct glyph_row *, ptrdiff_t);
  1118 static bool cursor_row_p (struct glyph_row *);
  1119 static int redisplay_mode_lines (Lisp_Object, bool);
  1120 
  1121 static void handle_line_prefix (struct it *);
  1122 
  1123 static void handle_stop_backwards (struct it *, ptrdiff_t);
  1124 static void unwind_with_echo_area_buffer (Lisp_Object);
  1125 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
  1126 static bool current_message_1 (void *, Lisp_Object);
  1127 static bool truncate_message_1 (void *, Lisp_Object);
  1128 static void set_message (Lisp_Object);
  1129 static bool set_message_1 (void *, Lisp_Object);
  1130 static bool display_echo_area_1 (void *, Lisp_Object);
  1131 static bool resize_mini_window_1 (void *, Lisp_Object);
  1132 static void unwind_redisplay (void);
  1133 static void extend_face_to_end_of_line (struct it *);
  1134 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
  1135 static void push_it (struct it *, struct text_pos *);
  1136 static void iterate_out_of_display_property (struct it *);
  1137 static void pop_it (struct it *);
  1138 static void redisplay_internal (void);
  1139 static void echo_area_display (bool);
  1140 static void block_buffer_flips (void);
  1141 static void unblock_buffer_flips (void);
  1142 static void redisplay_windows (Lisp_Object);
  1143 static void redisplay_window (Lisp_Object, bool);
  1144 static Lisp_Object redisplay_window_error (Lisp_Object);
  1145 static Lisp_Object redisplay_window_0 (Lisp_Object);
  1146 static Lisp_Object redisplay_window_1 (Lisp_Object);
  1147 static bool set_cursor_from_row (struct window *, struct glyph_row *,
  1148                                  struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
  1149                                  int, int);
  1150 static bool cursor_row_fully_visible_p (struct window *, bool, bool, bool);
  1151 static bool update_menu_bar (struct frame *, bool, bool);
  1152 static bool try_window_reusing_current_matrix (struct window *);
  1153 static int try_window_id (struct window *);
  1154 static void maybe_produce_line_number (struct it *);
  1155 static bool should_produce_line_number (struct it *);
  1156 static bool display_line (struct it *, int);
  1157 static int display_mode_lines (struct window *);
  1158 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
  1159 static int display_mode_element (struct it *, int, int, int, Lisp_Object,
  1160                                  Lisp_Object, bool);
  1161 static int store_mode_line_string (const char *, Lisp_Object, bool, int, int,
  1162                                    Lisp_Object);
  1163 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
  1164 static void display_menu_bar (struct window *);
  1165 static void display_tab_bar (struct window *);
  1166 static void update_tab_bar (struct frame *, bool);
  1167 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
  1168                                       ptrdiff_t *);
  1169 static void pint2str (register char *, register int, register ptrdiff_t);
  1170 
  1171 static int display_string (const char *, Lisp_Object, Lisp_Object,
  1172                            ptrdiff_t, ptrdiff_t, struct it *, int, int, int,
  1173                            int);
  1174 static void compute_line_metrics (struct it *);
  1175 static bool get_overlay_strings (struct it *, ptrdiff_t);
  1176 static bool get_overlay_strings_1 (struct it *, ptrdiff_t, bool);
  1177 static void next_overlay_string (struct it *);
  1178 static void reseat (struct it *, struct text_pos, bool);
  1179 static void reseat_1 (struct it *, struct text_pos, bool);
  1180 static bool next_element_from_display_vector (struct it *);
  1181 static bool next_element_from_string (struct it *);
  1182 static bool next_element_from_c_string (struct it *);
  1183 static bool next_element_from_buffer (struct it *);
  1184 static bool next_element_from_composition (struct it *);
  1185 static bool next_element_from_image (struct it *);
  1186 static bool next_element_from_stretch (struct it *);
  1187 static bool next_element_from_xwidget (struct it *);
  1188 static void load_overlay_strings (struct it *, ptrdiff_t);
  1189 static bool get_next_display_element (struct it *);
  1190 static enum move_it_result
  1191        move_it_in_display_line_to (struct it *, ptrdiff_t, int,
  1192                                    enum move_operation_enum);
  1193 static void get_visually_first_element (struct it *);
  1194 static void compute_stop_pos (struct it *);
  1195 static int face_before_or_after_it_pos (struct it *, bool);
  1196 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1197                                 Lisp_Object, struct text_pos *, ptrdiff_t, bool);
  1198 static int handle_single_display_spec (struct it *, Lisp_Object, Lisp_Object,
  1199                                        Lisp_Object, struct text_pos *,
  1200                                        ptrdiff_t, int, bool, bool);
  1201 static int underlying_face_id (const struct it *);
  1202 
  1203 #define face_before_it_pos(IT) face_before_or_after_it_pos (IT, true)
  1204 #define face_after_it_pos(IT)  face_before_or_after_it_pos (IT, false)
  1205 
  1206 #ifdef HAVE_WINDOW_SYSTEM
  1207 
  1208 static void update_tool_bar (struct frame *, bool);
  1209 static void gui_draw_bottom_divider (struct window *w);
  1210 static void notice_overwritten_cursor (struct window *,
  1211                                        enum glyph_row_area,
  1212                                        int, int, int, int);
  1213 static int  normal_char_height (struct font *, int);
  1214 static void normal_char_ascent_descent (struct font *, int, int *, int *);
  1215 
  1216 static void append_stretch_glyph (struct it *, Lisp_Object,
  1217                                   int, int, int);
  1218 
  1219 static Lisp_Object get_it_property (struct it *, Lisp_Object);
  1220 static Lisp_Object calc_line_height_property (struct it *, Lisp_Object,
  1221                                               struct font *, int, bool);
  1222 static int adjust_glyph_width_for_mouse_face (struct glyph *,
  1223                                               struct glyph_row *,
  1224                                               struct window *, struct face *,
  1225                                               struct face *);
  1226 static void get_cursor_offset_for_mouse_face (struct window *w,
  1227                                               struct glyph_row *row,
  1228                                               int *offset);
  1229 #endif /* HAVE_WINDOW_SYSTEM */
  1230 
  1231 static void produce_special_glyphs (struct it *, enum display_element_type);
  1232 static void pad_mode_line (struct it *, bool);
  1233 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
  1234 static bool coords_in_mouse_face_p (struct window *, int, int);
  1235 static void reset_box_start_end_flags (struct it *);
  1236 
  1237 
  1238 
  1239 /***********************************************************************
  1240                       Window display dimensions
  1241  ***********************************************************************/
  1242 
  1243 /* Return the bottom boundary y-position for text lines in window W.
  1244    This is the first y position at which a line cannot start.
  1245    It is relative to the top of the window.
  1246 
  1247    This is the height of W minus the height of a mode line, if any.  */
  1248 
  1249 int
  1250 window_text_bottom_y (struct window *w)
  1251 {
  1252   int height = WINDOW_PIXEL_HEIGHT (w);
  1253 
  1254   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1255 
  1256   if (window_wants_mode_line (w))
  1257     height -= CURRENT_MODE_LINE_HEIGHT (w);
  1258 
  1259   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1260 
  1261   return height;
  1262 }
  1263 
  1264 /* Return the pixel width of display area AREA of window W.
  1265    ANY_AREA means return the total width of W, not including
  1266    fringes to the left and right of the window.  */
  1267 
  1268 int
  1269 window_box_width (struct window *w, enum glyph_row_area area)
  1270 {
  1271   int width = w->pixel_width;
  1272 
  1273   if (!w->pseudo_window_p)
  1274     {
  1275       width -= WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  1276       width -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
  1277 
  1278       if (area == TEXT_AREA)
  1279         width -= (WINDOW_MARGINS_WIDTH (w)
  1280                    + WINDOW_FRINGES_WIDTH (w));
  1281       else if (area == LEFT_MARGIN_AREA)
  1282         width = WINDOW_LEFT_MARGIN_WIDTH (w);
  1283       else if (area == RIGHT_MARGIN_AREA)
  1284         width = WINDOW_RIGHT_MARGIN_WIDTH (w);
  1285     }
  1286 
  1287   /* With wide margins, fringes, etc. we might end up with a negative
  1288      width, correct that here.  */
  1289   return max (0, width);
  1290 }
  1291 
  1292 
  1293 /* Return the pixel height of the display area of window W, not
  1294    including mode lines of W, if any.  */
  1295 
  1296 int
  1297 window_box_height (struct window *w)
  1298 {
  1299   struct frame *f = XFRAME (w->frame);
  1300   int height = WINDOW_PIXEL_HEIGHT (w);
  1301 
  1302   eassert (height >= 0);
  1303 
  1304   height -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  1305   height -= WINDOW_SCROLL_BAR_AREA_HEIGHT (w);
  1306 
  1307   /* Note: the code below that determines the mode-line/header-line/tab-line
  1308      height is essentially the same as that contained in the macro
  1309      CURRENT_{MODE,HEADER,TAB}_LINE_HEIGHT, except that it checks whether
  1310      the appropriate glyph row has its `mode_line_p' flag set, and if
  1311      it doesn't, uses estimate_mode_line_height instead.  */
  1312 
  1313   if (window_wants_mode_line (w))
  1314     {
  1315       if (w->mode_line_height >= 0)
  1316         height -= w->mode_line_height;
  1317       else
  1318         {
  1319           struct glyph_row *ml_row
  1320             = (w->current_matrix && w->current_matrix->rows
  1321                ? MATRIX_MODE_LINE_ROW (w->current_matrix)
  1322                : 0);
  1323           if (ml_row && ml_row->mode_line_p)
  1324             height -= ml_row->height;
  1325           else
  1326             height -= estimate_mode_line_height
  1327               (f, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w));
  1328         }
  1329     }
  1330 
  1331   if (window_wants_tab_line (w))
  1332     {
  1333       if (w->tab_line_height >= 0)
  1334         height -= w->tab_line_height;
  1335       else
  1336         {
  1337           struct glyph_row *tl_row
  1338             = (w->current_matrix && w->current_matrix->rows
  1339                ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  1340                : 0);
  1341           if (tl_row && tl_row->mode_line_p)
  1342             height -= tl_row->height;
  1343           else
  1344             height -= estimate_mode_line_height (f, TAB_LINE_FACE_ID);
  1345         }
  1346     }
  1347 
  1348   if (window_wants_header_line (w))
  1349     {
  1350       if (w->header_line_height >= 0)
  1351         height -= w->header_line_height;
  1352       else
  1353         {
  1354           struct glyph_row *hl_row
  1355             = (w->current_matrix && w->current_matrix->rows
  1356                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  1357                : 0);
  1358           if (hl_row && hl_row->mode_line_p)
  1359             height -= hl_row->height;
  1360           else
  1361             height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
  1362         }
  1363     }
  1364 
  1365   /* With a very small font and a mode-line that's taller than
  1366      default, we might end up with a negative height.  */
  1367   return max (0, height);
  1368 }
  1369 
  1370 /* Return the window-relative coordinate of the left edge of display
  1371    area AREA of window W.  ANY_AREA means return the left edge of the
  1372    whole window, to the right of the left fringe of W.  */
  1373 
  1374 int
  1375 window_box_left_offset (struct window *w, enum glyph_row_area area)
  1376 {
  1377   int x;
  1378 
  1379   if (w->pseudo_window_p)
  1380     return 0;
  1381 
  1382   x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
  1383 
  1384   if (area == TEXT_AREA)
  1385     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1386           + window_box_width (w, LEFT_MARGIN_AREA));
  1387   else if (area == RIGHT_MARGIN_AREA)
  1388     x += (WINDOW_LEFT_FRINGE_WIDTH (w)
  1389           + window_box_width (w, LEFT_MARGIN_AREA)
  1390           + window_box_width (w, TEXT_AREA)
  1391           + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  1392              ? 0
  1393              : WINDOW_RIGHT_FRINGE_WIDTH (w)));
  1394   else if (area == LEFT_MARGIN_AREA
  1395            && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
  1396     x += WINDOW_LEFT_FRINGE_WIDTH (w);
  1397 
  1398   /* Don't return more than the window's pixel width.  */
  1399   return min (x, w->pixel_width);
  1400 }
  1401 
  1402 
  1403 /* Return the window-relative coordinate of the right edge of display
  1404    area AREA of window W.  ANY_AREA means return the right edge of the
  1405    whole window, to the left of the right fringe of W.  */
  1406 
  1407 static int
  1408 window_box_right_offset (struct window *w, enum glyph_row_area area)
  1409 {
  1410   /* Don't return more than the window's pixel width.  */
  1411   return min (window_box_left_offset (w, area) + window_box_width (w, area),
  1412               w->pixel_width);
  1413 }
  1414 
  1415 /* Return the frame-relative coordinate of the left edge of display
  1416    area AREA of window W.  ANY_AREA means return the left edge of the
  1417    whole window, to the right of the left fringe of W.  */
  1418 
  1419 int
  1420 window_box_left (struct window *w, enum glyph_row_area area)
  1421 {
  1422   struct frame *f = XFRAME (w->frame);
  1423   int x;
  1424 
  1425   if (w->pseudo_window_p)
  1426     return FRAME_INTERNAL_BORDER_WIDTH (f);
  1427 
  1428   x = (WINDOW_LEFT_EDGE_X (w)
  1429        + window_box_left_offset (w, area));
  1430 
  1431   return x;
  1432 }
  1433 
  1434 
  1435 /* Return the frame-relative coordinate of the right edge of display
  1436    area AREA of window W.  ANY_AREA means return the right edge of the
  1437    whole window, to the left of the right fringe of W.  */
  1438 
  1439 int
  1440 window_box_right (struct window *w, enum glyph_row_area area)
  1441 {
  1442   return window_box_left (w, area) + window_box_width (w, area);
  1443 }
  1444 
  1445 /* Get the bounding box of the display area AREA of window W, without
  1446    mode lines, in frame-relative coordinates.  ANY_AREA means the
  1447    whole window, not including the left and right fringes of
  1448    the window.  Return in *BOX_X and *BOX_Y the frame-relative pixel
  1449    coordinates of the upper-left corner of the box.  Return in
  1450    *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box.  */
  1451 
  1452 void
  1453 window_box (struct window *w, enum glyph_row_area area, int *box_x,
  1454             int *box_y, int *box_width, int *box_height)
  1455 {
  1456   if (box_width)
  1457     *box_width = window_box_width (w, area);
  1458   if (box_height)
  1459     *box_height = window_box_height (w);
  1460   if (box_x)
  1461     *box_x = window_box_left (w, area);
  1462   if (box_y)
  1463     {
  1464       *box_y = WINDOW_TOP_EDGE_Y (w);
  1465       if (window_wants_tab_line (w))
  1466         *box_y += CURRENT_TAB_LINE_HEIGHT (w);
  1467       if (window_wants_header_line (w))
  1468         *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
  1469     }
  1470 }
  1471 
  1472 #ifdef HAVE_WINDOW_SYSTEM
  1473 
  1474 /* Get the bounding box of the display area AREA of window W, without
  1475    mode lines and both fringes of the window.  Return in *TOP_LEFT_X
  1476    and TOP_LEFT_Y the frame-relative pixel coordinates of the
  1477    upper-left corner of the box.  Return in *BOTTOM_RIGHT_X, and
  1478    *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
  1479    box.  */
  1480 
  1481 static void
  1482 window_box_edges (struct window *w, int *top_left_x, int *top_left_y,
  1483                   int *bottom_right_x, int *bottom_right_y)
  1484 {
  1485   window_box (w, ANY_AREA, top_left_x, top_left_y,
  1486               bottom_right_x, bottom_right_y);
  1487   *bottom_right_x += *top_left_x;
  1488   *bottom_right_y += *top_left_y;
  1489 }
  1490 
  1491 #endif /* HAVE_WINDOW_SYSTEM */
  1492 
  1493 /***********************************************************************
  1494                               Utilities
  1495  ***********************************************************************/
  1496 
  1497 /* Return the bottom y-position of the line the iterator IT is in.
  1498    This can modify IT's settings.  */
  1499 
  1500 int
  1501 line_bottom_y (struct it *it)
  1502 {
  1503   int line_height = it->max_ascent + it->max_descent;
  1504   int line_top_y = it->current_y;
  1505 
  1506   if (line_height == 0)
  1507     {
  1508       if (last_height)
  1509         line_height = last_height;
  1510       else if (IT_CHARPOS (*it) < ZV)
  1511         {
  1512           move_it_by_lines (it, 1);
  1513           line_height = (it->max_ascent || it->max_descent
  1514                          ? it->max_ascent + it->max_descent
  1515                          : last_height);
  1516         }
  1517       else
  1518         {
  1519           struct glyph_row *row = it->glyph_row;
  1520 
  1521           /* Use the default character height.  */
  1522           it->glyph_row = NULL;
  1523           it->what = IT_CHARACTER;
  1524           it->c = ' ';
  1525           it->len = 1;
  1526           PRODUCE_GLYPHS (it);
  1527           line_height = it->ascent + it->descent;
  1528           it->glyph_row = row;
  1529         }
  1530     }
  1531 
  1532   return line_top_y + line_height;
  1533 }
  1534 
  1535 DEFUN ("line-pixel-height", Fline_pixel_height,
  1536        Sline_pixel_height, 0, 0, 0,
  1537        doc: /* Return height in pixels of text line in the selected window.
  1538 
  1539 Value is the height in pixels of the line at point.  */)
  1540   (void)
  1541 {
  1542   struct it it;
  1543   struct text_pos pt;
  1544   struct window *w = XWINDOW (selected_window);
  1545   struct buffer *old_buffer = NULL;
  1546   Lisp_Object result;
  1547 
  1548   if (XBUFFER (w->contents) != current_buffer)
  1549     {
  1550       old_buffer = current_buffer;
  1551       set_buffer_internal_1 (XBUFFER (w->contents));
  1552     }
  1553   SET_TEXT_POS (pt, PT, PT_BYTE);
  1554   void *itdata = bidi_shelve_cache ();
  1555   start_display (&it, w, pt);
  1556   /* Start from the beginning of the screen line, to make sure we
  1557      traverse all of its display elements, and thus capture the
  1558      correct metrics.  */
  1559   move_it_by_lines (&it, 0);
  1560   it.vpos = it.current_y = 0;
  1561   last_height = 0;
  1562   result = make_fixnum (line_bottom_y (&it));
  1563   if (old_buffer)
  1564     set_buffer_internal_1 (old_buffer);
  1565 
  1566   bidi_unshelve_cache (itdata, false);
  1567   return result;
  1568 }
  1569 
  1570 /* Return the default pixel height of text lines in window W.  The
  1571    value is the canonical height of the W frame's default font, plus
  1572    any extra space required by the line-spacing variable or frame
  1573    parameter.
  1574 
  1575    Implementation note: this ignores any line-spacing text properties
  1576    put on the newline characters.  This is because those properties
  1577    only affect the _screen_ line ending in the newline (i.e., in a
  1578    continued line, only the last screen line will be affected), which
  1579    means only a small number of lines in a buffer can ever use this
  1580    feature.  Since this function is used to compute the default pixel
  1581    equivalent of text lines in a window, we can safely ignore those
  1582    few lines.  For the same reasons, we ignore the line-height
  1583    properties.  */
  1584 int
  1585 default_line_pixel_height (struct window *w)
  1586 {
  1587   struct frame *f = WINDOW_XFRAME (w);
  1588   int height = FRAME_LINE_HEIGHT (f);
  1589 
  1590   if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
  1591     {
  1592       struct buffer *b = XBUFFER (w->contents);
  1593       Lisp_Object val = BVAR (b, extra_line_spacing);
  1594 
  1595       if (NILP (val))
  1596         val = BVAR (&buffer_defaults, extra_line_spacing);
  1597       if (!NILP (val))
  1598         {
  1599           if (RANGED_FIXNUMP (0, val, INT_MAX))
  1600             height += XFIXNAT (val);
  1601           else if (FLOATP (val))
  1602             {
  1603               int addon = XFLOAT_DATA (val) * height + 0.5;
  1604 
  1605               if (addon >= 0)
  1606                 height += addon;
  1607             }
  1608         }
  1609       else
  1610         height += f->extra_line_spacing;
  1611     }
  1612 
  1613   return height;
  1614 }
  1615 
  1616 /* Subroutine of pos_visible_p below.  Extracts a display string, if
  1617    any, from the display spec given as its argument.  */
  1618 static Lisp_Object
  1619 string_from_display_spec (Lisp_Object spec)
  1620 {
  1621   if (VECTORP (spec))
  1622     {
  1623       for (ptrdiff_t i = 0; i < ASIZE (spec); i++)
  1624         if (STRINGP (AREF (spec, i)))
  1625           return AREF (spec, i);
  1626     }
  1627   else
  1628     {
  1629       for (; CONSP (spec); spec = XCDR (spec))
  1630         if (STRINGP (XCAR (spec)))
  1631           return XCAR (spec);
  1632     }
  1633   return spec;
  1634 }
  1635 
  1636 
  1637 /* Limit insanely large values of W->hscroll on frame F to the largest
  1638    value that will still prevent first_visible_x and last_visible_x of
  1639    'struct it' from overflowing an int.  */
  1640 static int
  1641 window_hscroll_limited (struct window *w, struct frame *f)
  1642 {
  1643   ptrdiff_t window_hscroll = w->hscroll;
  1644   int window_text_width = window_box_width (w, TEXT_AREA);
  1645   int colwidth = FRAME_COLUMN_WIDTH (f);
  1646 
  1647   if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
  1648     window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
  1649 
  1650   return window_hscroll;
  1651 }
  1652 
  1653 /* Reset the box-face start and end flags in the iterator.  This is
  1654    called after producing glyphs, such that we reset these flags only
  1655    after producing a glyph with the flag set.  */
  1656 
  1657 static void
  1658 reset_box_start_end_flags (struct it *it)
  1659 {
  1660   /* Don't reset if we've drawn the glyph in the display margins --
  1661      those don't count as "produced glyphs".  */
  1662   if (it->area == TEXT_AREA
  1663       /* Don't reset if we displayed a fringe bitmap.  */
  1664       && !(it->what == IT_IMAGE && it->image_id < 0))
  1665     {
  1666       /* Don't reset if the face is not a box face: that might mean we
  1667          are iterating some overlay or display string, and the first
  1668          character to have the box face is yet to be seen, when we pop
  1669          the iterator stack. */
  1670       if (it->face_box_p)
  1671         it->start_of_box_run_p = false;
  1672       it->end_of_box_run_p = false;
  1673     }
  1674 }
  1675 
  1676 /* Return true if position CHARPOS is visible in window W.
  1677    CHARPOS < 0 means return info about WINDOW_END position.
  1678    If visible, set *X and *Y to pixel coordinates of top left corner.
  1679    Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
  1680    Set *ROWH and *VPOS to row's visible height and VPOS (row number).  */
  1681 
  1682 bool
  1683 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
  1684                int *rtop, int *rbot, int *rowh, int *vpos)
  1685 {
  1686   struct it it;
  1687   void *itdata = bidi_shelve_cache ();
  1688   struct text_pos top;
  1689   bool visible_p = false;
  1690   struct buffer *old_buffer = NULL;
  1691   bool r2l = false;
  1692 
  1693   if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
  1694     return visible_p;
  1695 
  1696   if (XBUFFER (w->contents) != current_buffer)
  1697     {
  1698       old_buffer = current_buffer;
  1699       set_buffer_internal_1 (XBUFFER (w->contents));
  1700     }
  1701 
  1702   SET_TEXT_POS_FROM_MARKER (top, w->start);
  1703   /* Scrolling a minibuffer window via scroll bar when the echo area
  1704      shows long text sometimes resets the minibuffer contents behind
  1705      our backs.  Also, someone might narrow-to-region and immediately
  1706      call a scroll function.  */
  1707   if (CHARPOS (top) > ZV || CHARPOS (top) < BEGV)
  1708     SET_TEXT_POS (top, BEGV, BEGV_BYTE);
  1709 
  1710   /* If the top of the window is after CHARPOS, the latter is surely
  1711      not visible.  */
  1712   if (charpos >= 0 && CHARPOS (top) > charpos)
  1713     return visible_p;
  1714 
  1715   /* Some Lisp hook could call us in the middle of redisplaying this
  1716      very window.  If, by some bad luck, we are retrying redisplay
  1717      because we found that the mode-line height and/or tab/header-line
  1718      height needs to be updated, the assignment of mode_line_height
  1719      and header_line_height below could disrupt that, due to the
  1720      selected/nonselected window dance during mode-line display, and
  1721      we could infloop.  Avoid that.  */
  1722   int prev_mode_line_height = w->mode_line_height;
  1723   int prev_header_line_height = w->header_line_height;
  1724   int prev_tab_line_height = w->tab_line_height;
  1725   /* Compute exact mode line heights.  */
  1726   if (window_wants_mode_line (w))
  1727     {
  1728       Lisp_Object window_mode_line_format
  1729         = window_parameter (w, Qmode_line_format);
  1730 
  1731       w->mode_line_height
  1732         = display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
  1733                              NILP (window_mode_line_format)
  1734                              ? BVAR (current_buffer, mode_line_format)
  1735                              : window_mode_line_format);
  1736     }
  1737 
  1738   if (window_wants_tab_line (w))
  1739     {
  1740       Lisp_Object window_tab_line_format
  1741         = window_parameter (w, Qtab_line_format);
  1742 
  1743       w->tab_line_height
  1744         = display_mode_line (w, TAB_LINE_FACE_ID,
  1745                              NILP (window_tab_line_format)
  1746                              ? BVAR (current_buffer, tab_line_format)
  1747                              : window_tab_line_format);
  1748     }
  1749 
  1750   if (window_wants_header_line (w))
  1751     {
  1752       Lisp_Object window_header_line_format
  1753         = window_parameter (w, Qheader_line_format);
  1754 
  1755       w->header_line_height
  1756         = display_mode_line (w, HEADER_LINE_FACE_ID,
  1757                              NILP (window_header_line_format)
  1758                              ? BVAR (current_buffer, header_line_format)
  1759                              : window_header_line_format);
  1760     }
  1761 
  1762   start_display (&it, w, top);
  1763   move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
  1764               (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
  1765 
  1766   /* Adjust for line numbers, if CHARPOS is at or beyond first_visible_x,
  1767      but we didn't yet produce the line-number glyphs.  */
  1768   if (!NILP (Vdisplay_line_numbers)
  1769       && it.current_x >= it.first_visible_x
  1770       && IT_CHARPOS (it) == charpos
  1771       && !it.line_number_produced_p)
  1772     {
  1773       /* If the pixel width of line numbers was not yet known, compute
  1774          it now.  This usually happens in the first display line of a
  1775          window.  */
  1776       if (!it.lnum_pixel_width)
  1777         {
  1778           struct it it2;
  1779           void *it2data = NULL;
  1780 
  1781           SAVE_IT (it2, it, it2data);
  1782           move_it_by_lines (&it, 1);
  1783           it2.lnum_pixel_width = it.lnum_pixel_width;
  1784           RESTORE_IT (&it, &it2, it2data);
  1785         }
  1786       it.current_x += it.lnum_pixel_width;
  1787     }
  1788 
  1789   if (charpos >= 0
  1790       && (((!it.bidi_p || it.bidi_it.scan_dir != -1)
  1791            && IT_CHARPOS (it) >= charpos)
  1792           /* When scanning backwards under bidi iteration, move_it_to
  1793              stops at or _before_ CHARPOS, because it stops at or to
  1794              the _right_ of the character at CHARPOS.  */
  1795           || (it.bidi_p && it.bidi_it.scan_dir == -1
  1796               && IT_CHARPOS (it) <= charpos)))
  1797     {
  1798       /* We have reached CHARPOS, or passed it.  How the call to
  1799          move_it_to can overshoot: (i) If CHARPOS is on invisible text
  1800          or covered by a display property, move_it_to stops at the end
  1801          of the invisible text, to the right of CHARPOS.  (ii) If
  1802          CHARPOS is in a display vector, move_it_to stops on its last
  1803          glyph.  */
  1804       int top_x = it.current_x;
  1805       int top_y = it.current_y;
  1806       int window_top_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  1807       int bottom_y;
  1808       struct it save_it;
  1809       void *save_it_data = NULL;
  1810 
  1811       /* Calling line_bottom_y may change it.method, it.position, etc.  */
  1812       SAVE_IT (save_it, it, save_it_data);
  1813       last_height = 0;
  1814       bottom_y = line_bottom_y (&it);
  1815       if (top_y < window_top_y)
  1816         visible_p = bottom_y > window_top_y;
  1817       else if (top_y < it.last_visible_y)
  1818         visible_p = true;
  1819       if (bottom_y >= it.last_visible_y
  1820           && it.bidi_p && it.bidi_it.scan_dir == -1
  1821           && IT_CHARPOS (it) < charpos)
  1822         {
  1823           /* When the last line of the window is scanned backwards
  1824              under bidi iteration, we could be duped into thinking
  1825              that we have passed CHARPOS, when in fact move_it_to
  1826              simply stopped short of CHARPOS because it reached
  1827              last_visible_y.  To see if that's what happened, we call
  1828              move_it_to again with a slightly larger vertical limit,
  1829              and see if it actually moved vertically; if it did, we
  1830              didn't really reach CHARPOS, which is beyond window end.  */
  1831           /* Why 10? because we don't know how many canonical lines
  1832              will the height of the next line(s) be.  So we guess.  */
  1833           int ten_more_lines = 10 * default_line_pixel_height (w);
  1834 
  1835           move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
  1836                       MOVE_TO_POS | MOVE_TO_Y);
  1837           if (it.current_y > top_y)
  1838             visible_p = false;
  1839 
  1840         }
  1841       RESTORE_IT (&it, &save_it, save_it_data);
  1842       if (visible_p)
  1843         {
  1844           if (it.method == GET_FROM_DISPLAY_VECTOR)
  1845             {
  1846               /* We stopped on the last glyph of a display vector.
  1847                  Try and recompute.  Hack alert!  */
  1848               if (charpos < 2 || top.charpos >= charpos)
  1849                 top_x = it.glyph_row->x;
  1850               else
  1851                 {
  1852                   struct it it2, it2_prev;
  1853                   /* The idea is to get to the previous buffer
  1854                      position, consume the character there, and use
  1855                      the pixel coordinates we get after that.  But if
  1856                      the previous buffer position is also displayed
  1857                      from a display vector, we need to consume all of
  1858                      the glyphs from that display vector.  */
  1859                   start_display (&it2, w, top);
  1860                   it2.glyph_row = NULL;
  1861                   move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
  1862                   /* If we didn't get to CHARPOS - 1, there's some
  1863                      replacing display property at that position, and
  1864                      we stopped after it.  That is exactly the place
  1865                      whose coordinates we want.  */
  1866                   if (IT_CHARPOS (it2) != charpos - 1)
  1867                     it2_prev = it2;
  1868                   else
  1869                     {
  1870                       /* Iterate until we get out of the display
  1871                          vector that displays the character at
  1872                          CHARPOS - 1.  */
  1873                       do {
  1874                         get_next_display_element (&it2);
  1875                         PRODUCE_GLYPHS (&it2);
  1876                         it2_prev = it2;
  1877                         set_iterator_to_next (&it2, true);
  1878                       } while (it2.method == GET_FROM_DISPLAY_VECTOR
  1879                                && IT_CHARPOS (it2) < charpos);
  1880                     }
  1881                   if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
  1882                       || it2_prev.current_x > it2_prev.last_visible_x)
  1883                     top_x = it.glyph_row->x;
  1884                   else
  1885                     {
  1886                       top_x = it2_prev.current_x;
  1887                       top_y = it2_prev.current_y;
  1888                     }
  1889                 }
  1890             }
  1891           else if (IT_CHARPOS (it) != charpos)
  1892             {
  1893               Lisp_Object cpos = make_fixnum (charpos);
  1894               Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  1895               Lisp_Object string = string_from_display_spec (spec);
  1896               struct text_pos tpos;
  1897               bool newline_in_string
  1898                 = (STRINGP (string)
  1899                    && memchr (SDATA (string), '\n', SBYTES (string)));
  1900 
  1901               SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
  1902               bool replacing_spec_p
  1903                 = (!NILP (spec)
  1904                    && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
  1905                                            charpos, FRAME_WINDOW_P (it.f)));
  1906               /* The tricky code below is needed because there's a
  1907                  discrepancy between move_it_to and how we set cursor
  1908                  when PT is at the beginning of a portion of text
  1909                  covered by a display property or an overlay with a
  1910                  display property, or the display line ends in a
  1911                  newline from a display string.  move_it_to will stop
  1912                  _after_ such display strings, whereas
  1913                  set_cursor_from_row conspires with cursor_row_p to
  1914                  place the cursor on the first glyph produced from the
  1915                  display string.  */
  1916 
  1917               /* We have overshoot PT because it is covered by a
  1918                  display property that replaces the text it covers.
  1919                  If the string includes embedded newlines, we are also
  1920                  in the wrong display line.  Backtrack to the correct
  1921                  line, where the display property begins.  */
  1922               if (replacing_spec_p)
  1923                 {
  1924                   Lisp_Object startpos, endpos;
  1925                   EMACS_INT start, end;
  1926                   struct it it3;
  1927 
  1928                   /* Find the first and the last buffer positions
  1929                      covered by the display string.  */
  1930                   endpos =
  1931                     Fnext_single_char_property_change (cpos, Qdisplay,
  1932                                                        Qnil, Qnil);
  1933                   startpos =
  1934                     Fprevious_single_char_property_change (endpos, Qdisplay,
  1935                                                            Qnil, Qnil);
  1936                   start = XFIXNAT (startpos);
  1937                   end = XFIXNAT (endpos);
  1938                   /* Move to the last buffer position before the
  1939                      display property.  */
  1940                   start_display (&it3, w, top);
  1941                   if (start > CHARPOS (top))
  1942                     move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
  1943                   /* Move forward one more line if the position before
  1944                      the display string is a newline or if it is the
  1945                      rightmost character on a line that is
  1946                      continued or word-wrapped.  */
  1947                   if (it3.method == GET_FROM_BUFFER
  1948                       && (it3.c == '\n'
  1949                           || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
  1950                     move_it_by_lines (&it3, 1);
  1951                   else if (move_it_in_display_line_to (&it3, -1,
  1952                                                        it3.current_x
  1953                                                        + it3.pixel_width,
  1954                                                        MOVE_TO_X)
  1955                            == MOVE_LINE_CONTINUED)
  1956                     {
  1957                       move_it_by_lines (&it3, 1);
  1958                       /* When we are under word-wrap, the #$@%!
  1959                          move_it_by_lines moves 2 lines, so we need to
  1960                          fix that up.  */
  1961                       if (it3.line_wrap == WORD_WRAP)
  1962                         move_it_by_lines (&it3, -1);
  1963                     }
  1964 
  1965                   /* Record the vertical coordinate of the display
  1966                      line where we wound up.  */
  1967                   top_y = it3.current_y;
  1968                   if (it3.bidi_p)
  1969                     {
  1970                       /* When characters are reordered for display,
  1971                          the character displayed to the left of the
  1972                          display string could be _after_ the display
  1973                          property in the logical order.  Use the
  1974                          smallest vertical position of these two.  */
  1975                       start_display (&it3, w, top);
  1976                       move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
  1977                       if (it3.current_y < top_y)
  1978                         top_y = it3.current_y;
  1979                     }
  1980                   /* Move from the top of the window to the beginning
  1981                      of the display line where the display string
  1982                      begins.  */
  1983                   start_display (&it3, w, top);
  1984                   it3.glyph_row = NULL;
  1985                   move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
  1986                   /* If it3_moved stays false after the 'while' loop
  1987                      below, that means we already were at a newline
  1988                      before the loop (e.g., the display string begins
  1989                      with a newline), so we don't need to return to
  1990                      the last position before the display string,
  1991                      because PRODUCE_GLYPHS will not produce anything
  1992                      for a newline.  */
  1993                   bool it3_moved = false;
  1994                   int top_x_before_string = it3.current_x;
  1995                   /* Finally, advance the iterator until we hit the
  1996                      first display element whose character position is
  1997                      at or beyond CHARPOS, or until the first newline
  1998                      from the display string, which signals the end of
  1999                      the display line.  */
  2000                   while (get_next_display_element (&it3))
  2001                     {
  2002                       if (!EQ (it3.object, string))
  2003                         top_x_before_string = it3.current_x;
  2004                       PRODUCE_GLYPHS (&it3);
  2005                       if ((it3.bidi_it.scan_dir == 1
  2006                            && IT_CHARPOS (it3) >= charpos)
  2007                           || (it3.bidi_it.scan_dir == -1
  2008                               && IT_CHARPOS (it3) <= charpos)
  2009                           || ITERATOR_AT_END_OF_LINE_P (&it3))
  2010                         break;
  2011                       it3_moved = true;
  2012                       set_iterator_to_next (&it3, false);
  2013                     }
  2014                   top_x = it3.current_x - it3.pixel_width;
  2015                   /* Account for line-number display, if IT3 still
  2016                      didn't.  This can happen if START - 1 is the
  2017                      first or the last character on its display line.  */
  2018                   if (!it3.line_number_produced_p)
  2019                     {
  2020                       if (it3.lnum_pixel_width > 0)
  2021                         {
  2022                           top_x += it3.lnum_pixel_width;
  2023                           top_x_before_string += it3.lnum_pixel_width;
  2024                         }
  2025                       else if (it.line_number_produced_p)
  2026                         {
  2027                           top_x += it.lnum_pixel_width;
  2028                           top_x_before_string += it3.lnum_pixel_width;
  2029                         }
  2030                     }
  2031                   /* Normally, we would exit the above loop because we
  2032                      found the display element whose character
  2033                      position is CHARPOS.  For the contingency that we
  2034                      didn't, and stopped at the first newline from the
  2035                      display string, reset top_x to the coordinate of
  2036                      the rightmost glyph not from the string.  */
  2037                   if (it3_moved
  2038                       && newline_in_string
  2039                       && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
  2040                     top_x = top_x_before_string;
  2041                 }
  2042             }
  2043 
  2044           *x = top_x;
  2045           /* The condition below is a heuristic fix for the situation
  2046              where move_it_to stops just after finishing the display
  2047              of a fringe bitmap, which resets it.ascent to zero, and
  2048              thus causes Y to be offset by it.max_ascent.  */
  2049           if (it.ascent == 0 && it.what == IT_IMAGE
  2050               && it.method != GET_FROM_IMAGE
  2051               && it.image_id < 0
  2052               && it.max_ascent > 0)
  2053             *y = max (top_y, window_top_y);
  2054           else
  2055             *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
  2056           *rtop = max (0, window_top_y - top_y);
  2057           *rbot = max (0, bottom_y - it.last_visible_y);
  2058           *rowh = max (0, (min (bottom_y, it.last_visible_y)
  2059                            - max (top_y, window_top_y)));
  2060           *vpos = it.vpos;
  2061           if (it.bidi_it.paragraph_dir == R2L)
  2062             r2l = true;
  2063         }
  2064     }
  2065   else
  2066     {
  2067       /* Either we were asked to provide info about WINDOW_END, or
  2068          CHARPOS is in the partially visible glyph row at end of
  2069          window.  */
  2070       struct it it2;
  2071       void *it2data = NULL;
  2072 
  2073       SAVE_IT (it2, it, it2data);
  2074       if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
  2075         move_it_by_lines (&it, 1);
  2076       if (charpos < IT_CHARPOS (it)
  2077           || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
  2078         {
  2079           visible_p = true;
  2080           RESTORE_IT (&it2, &it2, it2data);
  2081           move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
  2082           *x = it2.current_x;
  2083           if (it2.ascent == 0 && it2.what == IT_IMAGE
  2084               && it2.method != GET_FROM_IMAGE
  2085               && it2.image_id < 0
  2086               && it2.max_ascent > 0)
  2087             *y = it2.current_y;
  2088           else
  2089             *y = it2.current_y + it2.max_ascent - it2.ascent;
  2090           *rtop = max (0, -it2.current_y);
  2091           *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
  2092                            - it.last_visible_y));
  2093           *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
  2094                                 it.last_visible_y)
  2095                            - max (max (it2.current_y,
  2096                                        WINDOW_TAB_LINE_HEIGHT (w)),
  2097                                   WINDOW_HEADER_LINE_HEIGHT (w))));
  2098           *vpos = it2.vpos;
  2099           if (it2.bidi_it.paragraph_dir == R2L)
  2100             r2l = true;
  2101         }
  2102       else
  2103         bidi_unshelve_cache (it2data, true);
  2104     }
  2105   bidi_unshelve_cache (itdata, false);
  2106 
  2107   if (old_buffer)
  2108     set_buffer_internal_1 (old_buffer);
  2109 
  2110   if (visible_p)
  2111     {
  2112       if (w->hscroll > 0)
  2113         *x -=
  2114           window_hscroll_limited (w, WINDOW_XFRAME (w))
  2115           * WINDOW_FRAME_COLUMN_WIDTH (w);
  2116       /* For lines in an R2L paragraph, we need to mirror the X pixel
  2117          coordinate wrt the text area.  For the reasons, see the
  2118          commentary in buffer_posn_from_coords and the explanation of
  2119          the geometry used by the move_it_* functions at the end of
  2120          the large commentary near the beginning of this file.  */
  2121       if (r2l)
  2122         *x = window_box_width (w, TEXT_AREA) - *x - 1;
  2123     }
  2124 
  2125 #if false
  2126   /* Debugging code.  */
  2127   if (visible_p)
  2128     fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
  2129              charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
  2130   else
  2131     fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
  2132 #endif
  2133 
  2134   /* Restore potentially overwritten values.  */
  2135   w->mode_line_height = prev_mode_line_height;
  2136   w->header_line_height = prev_header_line_height;
  2137   w->tab_line_height = prev_tab_line_height;
  2138 
  2139   return visible_p;
  2140 }
  2141 
  2142 
  2143 /* Return the next character from STR.  Return in *LEN the length of
  2144    the character.  This is like string_char_and_length but never
  2145    returns an invalid character.  If we find one, we return a `?', but
  2146    with the length of the invalid character.  */
  2147 
  2148 static int
  2149 check_char_and_length (const unsigned char *str, int *len)
  2150 {
  2151   int c = string_char_and_length (str, len);
  2152   if (!CHAR_VALID_P (c))
  2153     /* We may not change the length here because other places in Emacs
  2154        don't use this function, i.e. they silently accept invalid
  2155        characters.  */
  2156     c = '?';
  2157 
  2158   return c;
  2159 }
  2160 
  2161 
  2162 
  2163 /* Given a position POS containing a valid character and byte position
  2164    in STRING, return the position NCHARS ahead (NCHARS >= 0).  */
  2165 
  2166 static struct text_pos
  2167 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
  2168 {
  2169   eassert (STRINGP (string) && nchars >= 0);
  2170 
  2171   if (STRING_MULTIBYTE (string))
  2172     {
  2173       const unsigned char *p = SDATA (string) + BYTEPOS (pos);
  2174 
  2175       while (nchars--)
  2176         {
  2177           int len = BYTES_BY_CHAR_HEAD (*p);
  2178           p += len;
  2179           CHARPOS (pos) += 1;
  2180           BYTEPOS (pos) += len;
  2181         }
  2182     }
  2183   else
  2184     SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
  2185 
  2186   return pos;
  2187 }
  2188 
  2189 
  2190 /* Value is the text position, i.e. character and byte position,
  2191    for character position CHARPOS in STRING.  */
  2192 
  2193 static struct text_pos
  2194 string_pos (ptrdiff_t charpos, Lisp_Object string)
  2195 {
  2196   struct text_pos pos;
  2197   eassert (STRINGP (string));
  2198   eassert (charpos >= 0);
  2199   SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
  2200   return pos;
  2201 }
  2202 
  2203 
  2204 /* Value is a text position, i.e. character and byte position, for
  2205    character position CHARPOS in C string S.  MULTIBYTE_P
  2206    means recognize multibyte characters.  */
  2207 
  2208 static struct text_pos
  2209 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
  2210 {
  2211   struct text_pos pos;
  2212 
  2213   eassert (s != NULL);
  2214   eassert (charpos >= 0);
  2215 
  2216   if (multibyte_p)
  2217     {
  2218       SET_TEXT_POS (pos, 0, 0);
  2219       while (charpos--)
  2220         {
  2221           int len = BYTES_BY_CHAR_HEAD (*s);
  2222           s += len;
  2223           CHARPOS (pos) += 1;
  2224           BYTEPOS (pos) += len;
  2225         }
  2226     }
  2227   else
  2228     SET_TEXT_POS (pos, charpos, charpos);
  2229 
  2230   return pos;
  2231 }
  2232 
  2233 
  2234 /* Value is the number of characters in C string S.  MULTIBYTE_P
  2235    means recognize multibyte characters.  */
  2236 
  2237 static ptrdiff_t
  2238 number_of_chars (const char *s, bool multibyte_p)
  2239 {
  2240   ptrdiff_t nchars;
  2241 
  2242   if (multibyte_p)
  2243     {
  2244       ptrdiff_t rest = strlen (s);
  2245       const unsigned char *p = (const unsigned char *) s;
  2246 
  2247       for (nchars = 0; rest > 0; ++nchars)
  2248         {
  2249           int len = BYTES_BY_CHAR_HEAD (*p);
  2250           rest -= len, p += len;
  2251         }
  2252     }
  2253   else
  2254     nchars = strlen (s);
  2255 
  2256   return nchars;
  2257 }
  2258 
  2259 
  2260 /* Compute byte position NEWPOS->bytepos corresponding to
  2261    NEWPOS->charpos.  POS is a known position in string STRING.
  2262    NEWPOS->charpos must be >= POS.charpos.  */
  2263 
  2264 static void
  2265 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
  2266 {
  2267   eassert (STRINGP (string));
  2268   eassert (CHARPOS (*newpos) >= CHARPOS (pos));
  2269 
  2270   if (STRING_MULTIBYTE (string))
  2271     *newpos = string_pos_nchars_ahead (pos, string,
  2272                                        CHARPOS (*newpos) - CHARPOS (pos));
  2273   else
  2274     BYTEPOS (*newpos) = CHARPOS (*newpos);
  2275 }
  2276 
  2277 /* EXPORT:
  2278    Return an estimation of the pixel height of mode or header lines on
  2279    frame F.  FACE_ID specifies what line's height to estimate.  */
  2280 
  2281 int
  2282 estimate_mode_line_height (struct frame *f, enum face_id face_id)
  2283 {
  2284 #ifdef HAVE_WINDOW_SYSTEM
  2285   if (FRAME_WINDOW_P (f))
  2286     {
  2287       int height = FONT_HEIGHT (FRAME_FONT (f));
  2288 
  2289       /* This function is called so early when Emacs starts that the face
  2290          cache and mode line face are not yet initialized.  */
  2291       if (FRAME_FACE_CACHE (f))
  2292         {
  2293           struct face *face = FACE_FROM_ID_OR_NULL (f, face_id);
  2294           if (face)
  2295             {
  2296               if (face->font)
  2297                 height = normal_char_height (face->font, -1);
  2298               if (face->box_horizontal_line_width > 0)
  2299                 height += 2 * face->box_horizontal_line_width;
  2300             }
  2301         }
  2302 
  2303       return height;
  2304     }
  2305 #endif
  2306 
  2307   return 1;
  2308 }
  2309 
  2310 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
  2311    coordinates in (*X, *Y).  Set *BOUNDS to the rectangle that the
  2312    glyph at X, Y occupies, if BOUNDS != 0.  If NOCLIP, do
  2313    not force the value into range.  */
  2314 
  2315 void
  2316 pixel_to_glyph_coords (struct frame *f, int pix_x, int pix_y, int *x, int *y,
  2317                        NativeRectangle *bounds, bool noclip)
  2318 {
  2319 
  2320 #ifdef HAVE_WINDOW_SYSTEM
  2321   if (FRAME_WINDOW_P (f))
  2322     {
  2323       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
  2324          even for negative values.  */
  2325       if (pix_x < 0)
  2326         pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
  2327       if (pix_y < 0)
  2328         pix_y -= FRAME_LINE_HEIGHT (f) - 1;
  2329 
  2330       pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
  2331       pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
  2332 
  2333       if (bounds)
  2334         STORE_NATIVE_RECT (*bounds,
  2335                            FRAME_COL_TO_PIXEL_X (f, pix_x),
  2336                            FRAME_LINE_TO_PIXEL_Y (f, pix_y),
  2337                            FRAME_COLUMN_WIDTH (f) - 1,
  2338                            FRAME_LINE_HEIGHT (f) - 1);
  2339 
  2340       /* PXW: Should we clip pixels before converting to columns/lines?  */
  2341       if (!noclip)
  2342         {
  2343           if (pix_x < 0)
  2344             pix_x = 0;
  2345           else if (pix_x > FRAME_TOTAL_COLS (f))
  2346             pix_x = FRAME_TOTAL_COLS (f);
  2347 
  2348           if (pix_y < 0)
  2349             pix_y = 0;
  2350           else if (pix_y > FRAME_TOTAL_LINES (f))
  2351             pix_y = FRAME_TOTAL_LINES (f);
  2352         }
  2353     }
  2354 #endif
  2355 
  2356   *x = pix_x;
  2357   *y = pix_y;
  2358 }
  2359 
  2360 
  2361 /* Find the glyph under window-relative coordinates X/Y in window W.
  2362    Consider only glyphs from buffer text, i.e. no glyphs from overlay
  2363    strings.  Return in *HPOS and *VPOS the row and column number of
  2364    the glyph found.  Return in *AREA the glyph area containing X.
  2365    Value is a pointer to the glyph found or null if X/Y is not on
  2366    text, or we can't tell because W's current matrix is not up to
  2367    date.  */
  2368 
  2369 struct glyph *
  2370 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
  2371                   int *dx, int *dy, int *area)
  2372 {
  2373   struct glyph *glyph, *end;
  2374   struct glyph_row *row = NULL;
  2375   int x0, i;
  2376 
  2377   /* Find row containing Y.  Give up if some row is not enabled.  */
  2378   for (i = 0; i < w->current_matrix->nrows; ++i)
  2379     {
  2380       row = MATRIX_ROW (w->current_matrix, i);
  2381       if (!row->enabled_p)
  2382         return NULL;
  2383       if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
  2384         break;
  2385     }
  2386 
  2387   *vpos = i;
  2388   *hpos = 0;
  2389 
  2390   /* Give up if Y is not in the window.  */
  2391   if (i == w->current_matrix->nrows)
  2392     return NULL;
  2393 
  2394   /* Get the glyph area containing X.  */
  2395   if (w->pseudo_window_p)
  2396     {
  2397       *area = TEXT_AREA;
  2398       x0 = 0;
  2399     }
  2400   else
  2401     {
  2402       if (x < window_box_left_offset (w, TEXT_AREA))
  2403         {
  2404           *area = LEFT_MARGIN_AREA;
  2405           x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
  2406         }
  2407       else if (x < window_box_right_offset (w, TEXT_AREA))
  2408         {
  2409           *area = TEXT_AREA;
  2410           x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
  2411         }
  2412       else
  2413         {
  2414           *area = RIGHT_MARGIN_AREA;
  2415           x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
  2416         }
  2417     }
  2418 
  2419   /* Find glyph containing X.  */
  2420   glyph = row->glyphs[*area];
  2421   end = glyph + row->used[*area];
  2422   x -= x0;
  2423   while (glyph < end && x >= glyph->pixel_width)
  2424     {
  2425       x -= glyph->pixel_width;
  2426       ++glyph;
  2427     }
  2428 
  2429   if (glyph == end)
  2430     return NULL;
  2431 
  2432   if (dx)
  2433     {
  2434       *dx = x;
  2435       *dy = y - (row->y + row->ascent - glyph->ascent);
  2436     }
  2437 
  2438   *hpos = glyph - row->glyphs[*area];
  2439   return glyph;
  2440 }
  2441 
  2442 /* Convert frame-relative x/y to coordinates relative to window W.
  2443    Takes pseudo-windows into account.  */
  2444 
  2445 static void
  2446 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
  2447 {
  2448   if (w->pseudo_window_p)
  2449     {
  2450       /* A pseudo-window is always full-width, and starts at the
  2451          left edge of the frame, plus a frame border.  */
  2452       struct frame *f = XFRAME (w->frame);
  2453       *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
  2454       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2455     }
  2456   else
  2457     {
  2458       *x -= WINDOW_LEFT_EDGE_X (w);
  2459       *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
  2460     }
  2461 }
  2462 
  2463 #ifdef HAVE_WINDOW_SYSTEM
  2464 
  2465 /* EXPORT:
  2466    Return in RECTS[] at most N clipping rectangles for glyph string S.
  2467    Return the number of stored rectangles.  */
  2468 
  2469 int
  2470 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
  2471 {
  2472   Emacs_Rectangle r;
  2473 
  2474   if (n <= 0)
  2475     return 0;
  2476 
  2477   if (s->row->full_width_p)
  2478     {
  2479       /* Draw full-width.  X coordinates are relative to S->w->left_col.  */
  2480       r.x = WINDOW_LEFT_EDGE_X (s->w);
  2481       if (s->row->mode_line_p)
  2482         r.width = WINDOW_PIXEL_WIDTH (s->w) - WINDOW_RIGHT_DIVIDER_WIDTH (s->w);
  2483       else
  2484         r.width = WINDOW_PIXEL_WIDTH (s->w);
  2485 
  2486       /* Unless displaying a mode or menu bar line, which are always
  2487          fully visible, clip to the visible part of the row.  */
  2488       if (s->w->pseudo_window_p)
  2489         r.height = s->row->visible_height;
  2490       else
  2491         r.height = s->height;
  2492     }
  2493   else
  2494     {
  2495       /* This is a text line that may be partially visible.  */
  2496       r.x = window_box_left (s->w, s->area);
  2497       r.width = window_box_width (s->w, s->area);
  2498       r.height = s->row->visible_height;
  2499     }
  2500 
  2501   if (s->clip_head)
  2502     if (r.x < s->clip_head->x)
  2503       {
  2504         if (r.width >= s->clip_head->x - r.x)
  2505           r.width -= s->clip_head->x - r.x;
  2506         else
  2507           r.width = 0;
  2508         r.x = s->clip_head->x;
  2509       }
  2510   if (s->clip_tail)
  2511     if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
  2512       {
  2513         if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
  2514           r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
  2515         else
  2516           r.width = 0;
  2517       }
  2518 
  2519   /* If S draws overlapping rows, it's sufficient to use the top and
  2520      bottom of the window for clipping because this glyph string
  2521      intentionally draws over other lines.  */
  2522   if (s->for_overlaps)
  2523     {
  2524       r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2525       r.height = window_text_bottom_y (s->w) - r.y;
  2526 
  2527       /* Alas, the above simple strategy does not work for the
  2528          environments with anti-aliased text: if the same text is
  2529          drawn onto the same place multiple times, it gets thicker.
  2530          If the overlap we are processing is for the erased cursor, we
  2531          take the intersection with the rectangle of the cursor.  */
  2532       if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
  2533         {
  2534           Emacs_Rectangle rc, r_save = r;
  2535 
  2536           rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
  2537           rc.y = s->w->phys_cursor.y;
  2538           rc.width = s->w->phys_cursor_width;
  2539           rc.height = s->w->phys_cursor_height;
  2540 
  2541           gui_intersect_rectangles (&r_save, &rc, &r);
  2542         }
  2543     }
  2544   else
  2545     {
  2546       /* Don't use S->y for clipping because it doesn't take partially
  2547          visible lines into account.  For example, it can be negative for
  2548          partially visible lines at the top of a window.  */
  2549       if (!s->row->full_width_p
  2550           && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
  2551         r.y = WINDOW_TAB_LINE_HEIGHT (s->w) + WINDOW_HEADER_LINE_HEIGHT (s->w);
  2552       else
  2553         r.y = max (0, s->row->y);
  2554     }
  2555 
  2556   r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
  2557 
  2558   /* If drawing the cursor, don't let glyph draw outside its
  2559      advertised boundaries. Cleartype does this under some circumstances.  */
  2560   if (s->hl == DRAW_CURSOR)
  2561     {
  2562       struct glyph *glyph = s->first_glyph;
  2563       int height, max_y;
  2564 
  2565       if (s->x > r.x)
  2566         {
  2567           if (r.width >= s->x - r.x)
  2568             r.width -= s->x - r.x;
  2569           else  /* R2L hscrolled row with cursor outside text area */
  2570             r.width = 0;
  2571           r.x = s->x;
  2572         }
  2573       r.width = min (r.width, glyph->pixel_width);
  2574 
  2575       /* If r.y is below window bottom, ensure that we still see a cursor.  */
  2576       height = min (glyph->ascent + glyph->descent,
  2577                     min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
  2578       max_y = window_text_bottom_y (s->w) - height;
  2579       max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
  2580       if (s->ybase - glyph->ascent > max_y)
  2581         {
  2582           r.y = max_y;
  2583           r.height = height;
  2584         }
  2585       else
  2586         {
  2587           /* Don't draw cursor glyph taller than our actual glyph.  */
  2588           height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
  2589           if (height < r.height)
  2590             {
  2591               max_y = r.y + r.height;
  2592               r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
  2593               r.height = min (max_y - r.y, height);
  2594             }
  2595         }
  2596     }
  2597 
  2598   if (s->row->clip)
  2599     {
  2600       Emacs_Rectangle r_save = r;
  2601 
  2602       if (! gui_intersect_rectangles (&r_save, s->row->clip, &r))
  2603         r.width = 0;
  2604     }
  2605 
  2606   if ((s->for_overlaps & OVERLAPS_BOTH) == 0
  2607       || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
  2608     {
  2609 #ifdef CONVERT_FROM_EMACS_RECT
  2610       CONVERT_FROM_EMACS_RECT (r, *rects);
  2611 #else
  2612       *rects = r;
  2613 #endif
  2614       return 1;
  2615     }
  2616   else
  2617     {
  2618       /* If we are processing overlapping and allowed to return
  2619          multiple clipping rectangles, we exclude the row of the glyph
  2620          string from the clipping rectangle.  This is to avoid drawing
  2621          the same text on the environment with anti-aliasing.  */
  2622 #ifdef CONVERT_FROM_EMACS_RECT
  2623       Emacs_Rectangle rs[2];
  2624 #else
  2625       Emacs_Rectangle *rs = rects;
  2626 #endif
  2627       int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
  2628 
  2629       if (s->for_overlaps & OVERLAPS_PRED)
  2630         {
  2631           rs[i] = r;
  2632           if (r.y + r.height > row_y)
  2633             {
  2634               if (r.y < row_y)
  2635                 rs[i].height = row_y - r.y;
  2636               else
  2637                 rs[i].height = 0;
  2638             }
  2639           i++;
  2640         }
  2641       if (s->for_overlaps & OVERLAPS_SUCC)
  2642         {
  2643           rs[i] = r;
  2644           if (r.y < row_y + s->row->visible_height)
  2645             {
  2646               if (r.y + r.height > row_y + s->row->visible_height)
  2647                 {
  2648                   rs[i].y = row_y + s->row->visible_height;
  2649                   rs[i].height = r.y + r.height - rs[i].y;
  2650                 }
  2651               else
  2652                 rs[i].height = 0;
  2653             }
  2654           i++;
  2655         }
  2656 
  2657       n = i;
  2658 #ifdef CONVERT_FROM_EMACS_RECT
  2659       for (i = 0; i < n; i++)
  2660         CONVERT_FROM_EMACS_RECT (rs[i], rects[i]);
  2661 #endif
  2662       return n;
  2663     }
  2664 }
  2665 
  2666 /* EXPORT:
  2667    Return in *NR the clipping rectangle for glyph string S.  */
  2668 
  2669 void
  2670 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
  2671 {
  2672   get_glyph_string_clip_rects (s, nr, 1);
  2673 }
  2674 
  2675 
  2676 /* EXPORT:
  2677    Return the position and height of the phys cursor in window W.
  2678    Set w->phys_cursor_width to width of phys cursor.
  2679 */
  2680 
  2681 void
  2682 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
  2683                           struct glyph *glyph, int *xp, int *yp, int *heightp)
  2684 {
  2685   struct frame *f = XFRAME (WINDOW_FRAME (w));
  2686   int x, y, wd, h, h0, y0, ascent;
  2687 
  2688   /* Compute the width of the rectangle to draw.  If on a stretch
  2689      glyph, and `x-stretch-cursor' is nil, don't draw a rectangle
  2690      as wide as the glyph, but use a canonical character width
  2691      instead.  */
  2692   wd = glyph->pixel_width;
  2693 
  2694   x = w->phys_cursor.x;
  2695   if (x < 0)
  2696     {
  2697       wd += x;
  2698       x = 0;
  2699     }
  2700 
  2701   if (glyph->type == STRETCH_GLYPH
  2702       && !x_stretch_cursor_p)
  2703     wd = min (FRAME_COLUMN_WIDTH (f), wd);
  2704   w->phys_cursor_width = wd;
  2705 
  2706   /* Don't let the hollow cursor glyph descend below the glyph row's
  2707      ascent value, lest the hollow cursor looks funny.  */
  2708   y = w->phys_cursor.y;
  2709   ascent = row->ascent;
  2710   /* The test for row at ZV is for when line numbers are displayed and
  2711      point is at EOB: the cursor could then be smaller or larger than
  2712      the default face's font.  */
  2713   if (!row->ends_at_zv_p && row->ascent < glyph->ascent)
  2714     {
  2715       y -= glyph->ascent - row->ascent;
  2716       ascent = glyph->ascent;
  2717     }
  2718 
  2719   /* If y is below window bottom, ensure that we still see a cursor.  */
  2720   h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
  2721 
  2722   h = max (h0, ascent + glyph->descent);
  2723   /* Don't let the cursor exceed the dimensions of the row, so that
  2724      the upper/lower side of the box aren't clipped.  */
  2725   h = min (h, row->height);
  2726   h0 = min (h0, ascent + glyph->descent);
  2727 
  2728   y0 = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  2729   if (y < y0)
  2730     {
  2731       h = max (h - (y0 - y) + 1, h0);
  2732       y = y0 - 1;
  2733     }
  2734   else
  2735     {
  2736       y0 = window_text_bottom_y (w) - h0;
  2737       if (y > y0)
  2738         {
  2739           h += y - y0;
  2740           y = y0;
  2741         }
  2742     }
  2743 
  2744   *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
  2745   *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
  2746   *heightp = h;
  2747 }
  2748 
  2749 /*
  2750  * Remember which glyph the mouse is over.
  2751  */
  2752 
  2753 void
  2754 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
  2755 {
  2756   Lisp_Object window;
  2757   struct window *w;
  2758   struct glyph_row *r, *gr, *end_row;
  2759   enum window_part part;
  2760   enum glyph_row_area area;
  2761   int x, y, width, height;
  2762 
  2763   if (mouse_fine_grained_tracking)
  2764     {
  2765       STORE_NATIVE_RECT (*rect, gx, gy, 1, 1);
  2766       return;
  2767     }
  2768 
  2769   /* Try to determine frame pixel position and size of the glyph under
  2770      frame pixel coordinates X/Y on frame F.  */
  2771 
  2772   if (window_resize_pixelwise)
  2773     {
  2774       width = height = 1;
  2775       goto virtual_glyph;
  2776     }
  2777   else if (!f->glyphs_initialized_p
  2778            || (window = window_from_coordinates (f, gx, gy, &part, false, false),
  2779                NILP (window)))
  2780     {
  2781       width = FRAME_SMALLEST_CHAR_WIDTH (f);
  2782       height = FRAME_SMALLEST_FONT_HEIGHT (f);
  2783       goto virtual_glyph;
  2784     }
  2785 
  2786   w = XWINDOW (window);
  2787   width = WINDOW_FRAME_COLUMN_WIDTH (w);
  2788   height = WINDOW_FRAME_LINE_HEIGHT (w);
  2789 
  2790   x = window_relative_x_coord (w, part, gx);
  2791   y = gy - WINDOW_TOP_EDGE_Y (w);
  2792 
  2793   r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
  2794   end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
  2795 
  2796   if (w->pseudo_window_p)
  2797     {
  2798       area = TEXT_AREA;
  2799       part = ON_MODE_LINE; /* Don't adjust margin. */
  2800       goto text_glyph;
  2801     }
  2802 
  2803   switch (part)
  2804     {
  2805     case ON_LEFT_MARGIN:
  2806       area = LEFT_MARGIN_AREA;
  2807       goto text_glyph;
  2808 
  2809     case ON_RIGHT_MARGIN:
  2810       area = RIGHT_MARGIN_AREA;
  2811       goto text_glyph;
  2812 
  2813     case ON_TAB_LINE:
  2814     case ON_HEADER_LINE:
  2815     case ON_MODE_LINE:
  2816       gr = (part == ON_TAB_LINE
  2817             ? MATRIX_TAB_LINE_ROW (w->current_matrix)
  2818             : (part == ON_HEADER_LINE
  2819                ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
  2820                : MATRIX_MODE_LINE_ROW (w->current_matrix)));
  2821       gy = gr->y;
  2822       area = TEXT_AREA;
  2823       goto text_glyph_row_found;
  2824 
  2825     case ON_TEXT:
  2826       area = TEXT_AREA;
  2827 
  2828     text_glyph:
  2829       gr = 0; gy = 0;
  2830       for (; r <= end_row && r->enabled_p; ++r)
  2831         if (r->y + r->height > y)
  2832           {
  2833             gr = r; gy = r->y;
  2834             break;
  2835           }
  2836 
  2837     text_glyph_row_found:
  2838       if (gr && gy <= y)
  2839         {
  2840           struct glyph *g = gr->glyphs[area];
  2841           struct glyph *end = g + gr->used[area];
  2842 
  2843           height = gr->height;
  2844           for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
  2845             if (gx + g->pixel_width > x)
  2846               break;
  2847 
  2848           if (g < end)
  2849             {
  2850               if (g->type == IMAGE_GLYPH)
  2851                 {
  2852                   /* Don't remember when mouse is over image, as
  2853                      image may have hot-spots.  */
  2854                   STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
  2855                   return;
  2856                 }
  2857               width = g->pixel_width;
  2858             }
  2859           else
  2860             {
  2861               /* Use nominal char spacing at end of line.  */
  2862               x -= gx;
  2863               gx += (x / width) * width;
  2864             }
  2865 
  2866           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2867               && part != ON_TAB_LINE)
  2868             {
  2869               gx += window_box_left_offset (w, area);
  2870               /* Don't expand over the modeline to make sure the vertical
  2871                  drag cursor is shown early enough.  */
  2872               height = min (height,
  2873                             max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2874             }
  2875         }
  2876       else
  2877         {
  2878           /* Use nominal line height at end of window.  */
  2879           gx = (x / width) * width;
  2880           y -= gy;
  2881           gy += (y / height) * height;
  2882           if (part != ON_MODE_LINE && part != ON_HEADER_LINE
  2883               && part != ON_TAB_LINE)
  2884             /* See comment above.  */
  2885             height = min (height,
  2886                           max (0, WINDOW_BOX_HEIGHT_NO_MODE_LINE (w) - gy));
  2887         }
  2888       break;
  2889 
  2890     case ON_LEFT_FRINGE:
  2891       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2892             ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
  2893             : window_box_right_offset (w, LEFT_MARGIN_AREA));
  2894       width = WINDOW_LEFT_FRINGE_WIDTH (w);
  2895       goto row_glyph;
  2896 
  2897     case ON_RIGHT_FRINGE:
  2898       gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2899             ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2900             : window_box_right_offset (w, TEXT_AREA));
  2901       if (WINDOW_RIGHT_DIVIDER_WIDTH (w) == 0
  2902           && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
  2903           && !WINDOW_RIGHTMOST_P (w))
  2904         if (gx < WINDOW_PIXEL_WIDTH (w) - width)
  2905           /* Make sure the vertical border can get her own glyph to the
  2906              right of the one we build here.  */
  2907           width = WINDOW_RIGHT_FRINGE_WIDTH (w) - width;
  2908         else
  2909           width = WINDOW_PIXEL_WIDTH (w) - gx;
  2910       else
  2911         width = WINDOW_RIGHT_FRINGE_WIDTH (w);
  2912 
  2913       goto row_glyph;
  2914 
  2915     case ON_VERTICAL_BORDER:
  2916       gx = WINDOW_PIXEL_WIDTH (w) - width;
  2917       goto row_glyph;
  2918 
  2919     case ON_VERTICAL_SCROLL_BAR:
  2920       gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
  2921             ? 0
  2922             : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
  2923                + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
  2924                   ? WINDOW_RIGHT_FRINGE_WIDTH (w)
  2925                   : 0)));
  2926       width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
  2927 
  2928     row_glyph:
  2929       gr = 0, gy = 0;
  2930       for (; r <= end_row && r->enabled_p; ++r)
  2931         if (r->y + r->height > y)
  2932           {
  2933             gr = r; gy = r->y;
  2934             break;
  2935           }
  2936 
  2937       if (gr && gy <= y)
  2938         height = gr->height;
  2939       else
  2940         {
  2941           /* Use nominal line height at end of window.  */
  2942           y -= gy;
  2943           gy += (y / height) * height;
  2944         }
  2945       break;
  2946 
  2947     case ON_RIGHT_DIVIDER:
  2948       gx = WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2949       width = WINDOW_RIGHT_DIVIDER_WIDTH (w);
  2950       gy = 0;
  2951       /* The bottom divider prevails. */
  2952       height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2953       goto add_edge;
  2954 
  2955     case ON_BOTTOM_DIVIDER:
  2956       gx = 0;
  2957       width = WINDOW_PIXEL_WIDTH (w);
  2958       gy = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2959       height = WINDOW_BOTTOM_DIVIDER_WIDTH (w);
  2960       goto add_edge;
  2961 
  2962     default:
  2963       ;
  2964     virtual_glyph:
  2965       /* If there is no glyph under the mouse, then we divide the screen
  2966          into a grid of the smallest glyph in the frame, and use that
  2967          as our "glyph".  */
  2968 
  2969       /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
  2970          round down even for negative values.  */
  2971       if (gx < 0)
  2972         gx -= width - 1;
  2973       if (gy < 0)
  2974         gy -= height - 1;
  2975 
  2976       gx = (gx / width) * width;
  2977       gy = (gy / height) * height;
  2978 
  2979       goto store_rect;
  2980     }
  2981 
  2982  add_edge:
  2983   gx += WINDOW_LEFT_EDGE_X (w);
  2984   gy += WINDOW_TOP_EDGE_Y (w);
  2985 
  2986  store_rect:
  2987   STORE_NATIVE_RECT (*rect, gx, gy, width, height);
  2988 
  2989   /* Visible feedback for debugging.  */
  2990 #if false && defined HAVE_X_WINDOWS
  2991   XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_DRAWABLE (f),
  2992                   f->output_data.x->normal_gc,
  2993                   gx, gy, width, height);
  2994 #endif
  2995 }
  2996 
  2997 
  2998 #endif /* HAVE_WINDOW_SYSTEM */
  2999 
  3000 static void
  3001 adjust_window_ends (struct window *w, struct glyph_row *row, bool current)
  3002 {
  3003   eassert (w);
  3004   w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
  3005   w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
  3006   w->window_end_vpos
  3007     = MATRIX_ROW_VPOS (row, current ? w->current_matrix : w->desired_matrix);
  3008 }
  3009 
  3010 static bool
  3011 hscrolling_current_line_p (struct window *w)
  3012 {
  3013   return (!w->suspend_auto_hscroll
  3014           && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
  3015                  Qcurrent_line));
  3016 }
  3017 
  3018 /***********************************************************************
  3019                         Lisp form evaluation
  3020  ***********************************************************************/
  3021 
  3022 /* Error handler for safe_eval and safe_call.  */
  3023 
  3024 static Lisp_Object
  3025 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
  3026 {
  3027   add_to_log ("Error during redisplay: %S signaled %S",
  3028               Flist (nargs, args), arg);
  3029   return Qnil;
  3030 }
  3031 
  3032 /* Call function FUNC with the rest of NARGS - 1 arguments
  3033    following.  Return the result, or nil if something went
  3034    wrong.  Prevent redisplay during the evaluation.  */
  3035 
  3036 static Lisp_Object
  3037 safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
  3038 {
  3039   Lisp_Object val;
  3040 
  3041   if (inhibit_eval_during_redisplay)
  3042     val = Qnil;
  3043   else
  3044     {
  3045       ptrdiff_t i;
  3046       specpdl_ref count = SPECPDL_INDEX ();
  3047       Lisp_Object *args;
  3048       USE_SAFE_ALLOCA;
  3049       SAFE_ALLOCA_LISP (args, nargs);
  3050 
  3051       args[0] = func;
  3052       for (i = 1; i < nargs; i++)
  3053         args[i] = va_arg (ap, Lisp_Object);
  3054 
  3055       specbind (Qinhibit_redisplay, Qt);
  3056       if (inhibit_quit)
  3057         specbind (Qinhibit_quit, Qt);
  3058       /* Use Qt to ensure debugger does not run,
  3059          so there is no possibility of wanting to redisplay.  */
  3060       val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
  3061                                        safe_eval_handler);
  3062       val = SAFE_FREE_UNBIND_TO (count, val);
  3063     }
  3064 
  3065   return val;
  3066 }
  3067 
  3068 Lisp_Object
  3069 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
  3070 {
  3071   Lisp_Object retval;
  3072   va_list ap;
  3073 
  3074   va_start (ap, func);
  3075   retval = safe__call (false, nargs, func, ap);
  3076   va_end (ap);
  3077   return retval;
  3078 }
  3079 
  3080 /* Call function FN with one argument ARG.
  3081    Return the result, or nil if something went wrong.  */
  3082 
  3083 Lisp_Object
  3084 safe_call1 (Lisp_Object fn, Lisp_Object arg)
  3085 {
  3086   return safe_call (2, fn, arg);
  3087 }
  3088 
  3089 static Lisp_Object
  3090 safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
  3091 {
  3092   Lisp_Object retval;
  3093   va_list ap;
  3094 
  3095   va_start (ap, fn);
  3096   retval = safe__call (inhibit_quit, 2, fn, ap);
  3097   va_end (ap);
  3098   return retval;
  3099 }
  3100 
  3101 Lisp_Object
  3102 safe_eval (Lisp_Object sexpr)
  3103 {
  3104   return safe__call1 (false, Qeval, sexpr);
  3105 }
  3106 
  3107 static Lisp_Object
  3108 safe__eval (bool inhibit_quit, Lisp_Object sexpr)
  3109 {
  3110   return safe__call1 (inhibit_quit, Qeval, sexpr);
  3111 }
  3112 
  3113 /* Call function FN with two arguments ARG1 and ARG2.
  3114    Return the result, or nil if something went wrong.  */
  3115 
  3116 Lisp_Object
  3117 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
  3118 {
  3119   return safe_call (3, fn, arg1, arg2);
  3120 }
  3121 
  3122 
  3123 
  3124 /***********************************************************************
  3125                               Debugging
  3126  ***********************************************************************/
  3127 
  3128 /* Define CHECK_IT to perform sanity checks on iterators.
  3129    This is for debugging.  It is too slow to do unconditionally.  */
  3130 
  3131 static void
  3132 CHECK_IT (struct it *it)
  3133 {
  3134 #if false
  3135   if (it->method == GET_FROM_STRING)
  3136     {
  3137       eassert (STRINGP (it->string));
  3138       eassert (IT_STRING_CHARPOS (*it) >= 0);
  3139     }
  3140   else
  3141     {
  3142       eassert (IT_STRING_CHARPOS (*it) < 0);
  3143       if (it->method == GET_FROM_BUFFER)
  3144         {
  3145           /* Check that character and byte positions agree.  */
  3146           eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
  3147         }
  3148     }
  3149 
  3150   if (it->dpvec)
  3151     eassert (it->current.dpvec_index >= 0);
  3152   else
  3153     eassert (it->current.dpvec_index < 0);
  3154 #endif
  3155 }
  3156 
  3157 
  3158 /* Check that the window end of window W is what we expect it
  3159    to be---the last row in the current matrix displaying text.  */
  3160 
  3161 static void
  3162 CHECK_WINDOW_END (struct window *w)
  3163 {
  3164 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
  3165   if (!MINI_WINDOW_P (w) && w->window_end_valid)
  3166     {
  3167       struct glyph_row *row;
  3168       eassert ((row = MATRIX_ROW (w->current_matrix, w->window_end_vpos),
  3169                 !row->enabled_p
  3170                 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
  3171                 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
  3172     }
  3173 #endif
  3174 }
  3175 
  3176 /***********************************************************************
  3177                        Iterator initialization
  3178  ***********************************************************************/
  3179 
  3180 /* Initialize IT for displaying current_buffer in window W, starting
  3181    at character position CHARPOS.  CHARPOS < 0 means that no buffer
  3182    position is specified which is useful when the iterator is assigned
  3183    a position later.  BYTEPOS is the byte position corresponding to
  3184    CHARPOS.
  3185 
  3186    If ROW is not null, calls to produce_glyphs with IT as parameter
  3187    will produce glyphs in that row.
  3188 
  3189    BASE_FACE_ID is the id of a base face to use.  It must be one of
  3190    DEFAULT_FACE_ID for normal text, MODE_LINE_ACTIVE_FACE_ID,
  3191    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
  3192    mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
  3193 
  3194    If ROW is null and BASE_FACE_ID is equal to MODE_LINE_ACTIVE_FACE_ID,
  3195    MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
  3196    will be initialized to use the corresponding mode line glyph row of
  3197    the desired matrix of W.  */
  3198 
  3199 void
  3200 init_iterator (struct it *it, struct window *w,
  3201                ptrdiff_t charpos, ptrdiff_t bytepos,
  3202                struct glyph_row *row, enum face_id base_face_id)
  3203 {
  3204   enum face_id remapped_base_face_id = base_face_id;
  3205   int body_width = 0, body_height = 0;
  3206 
  3207   /* Some precondition checks.  */
  3208   eassert (w != NULL && it != NULL);
  3209   eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
  3210                            && charpos <= ZV));
  3211 
  3212   /* If face attributes have been changed since the last redisplay,
  3213      free realized faces now because they depend on face definitions
  3214      that might have changed.  Don't free faces while there might be
  3215      desired matrices pending which reference these faces.  */
  3216   if (!inhibit_free_realized_faces)
  3217     {
  3218       if (face_change)
  3219         {
  3220           face_change = false;
  3221           XFRAME (w->frame)->face_change = 0;
  3222           free_all_realized_faces (Qnil);
  3223         }
  3224       else if (XFRAME (w->frame)->face_change)
  3225         {
  3226           XFRAME (w->frame)->face_change = 0;
  3227           free_all_realized_faces (w->frame);
  3228         }
  3229     }
  3230 
  3231   /* Perhaps remap BASE_FACE_ID to a user-specified alternative.  */
  3232   if (! NILP (Vface_remapping_alist))
  3233     remapped_base_face_id
  3234       = lookup_basic_face (w, XFRAME (w->frame), base_face_id);
  3235 
  3236   /* Use one of the mode line rows of W's desired matrix if
  3237      appropriate.  */
  3238   if (row == NULL)
  3239     {
  3240       if (base_face_id == MODE_LINE_ACTIVE_FACE_ID
  3241           || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
  3242         row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
  3243       else if (base_face_id == TAB_LINE_FACE_ID)
  3244         row = MATRIX_TAB_LINE_ROW (w->desired_matrix);
  3245       else if (base_face_id == HEADER_LINE_FACE_ID)
  3246         {
  3247           /* Header line row depends on whether tab line is enabled.  */
  3248           w->desired_matrix->tab_line_p = window_wants_tab_line (w);
  3249           row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
  3250         }
  3251     }
  3252 
  3253   /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
  3254      Other parts of redisplay rely on that.  */
  3255   memclear (it, sizeof *it);
  3256   it->current.overlay_string_index = -1;
  3257   it->current.dpvec_index = -1;
  3258   it->base_face_id = remapped_base_face_id;
  3259   IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  3260   it->paragraph_embedding = L2R;
  3261   it->bidi_it.w = w;
  3262 
  3263   /* The window in which we iterate over current_buffer:  */
  3264   XSETWINDOW (it->window, w);
  3265   it->w = w;
  3266   it->f = XFRAME (w->frame);
  3267 
  3268   it->cmp_it.id = -1;
  3269   it->cmp_it.parent_it = it;
  3270 
  3271   if (max_redisplay_ticks > 0)
  3272     update_redisplay_ticks (0, w);
  3273 
  3274   /* Extra space between lines (on window systems only).  */
  3275   if (base_face_id == DEFAULT_FACE_ID
  3276       && FRAME_WINDOW_P (it->f))
  3277     {
  3278       if (FIXNATP (BVAR (current_buffer, extra_line_spacing)))
  3279         it->extra_line_spacing = XFIXNAT (BVAR (current_buffer, extra_line_spacing));
  3280       else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
  3281         it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
  3282                                   * FRAME_LINE_HEIGHT (it->f));
  3283       else if (it->f->extra_line_spacing > 0)
  3284         it->extra_line_spacing = it->f->extra_line_spacing;
  3285     }
  3286 
  3287   /* If realized faces have been removed, e.g. because of face
  3288      attribute changes of named faces, recompute them.  When running
  3289      in batch mode, the face cache of the initial frame is null.  If
  3290      we happen to get called, make a dummy face cache.  */
  3291   if (FRAME_FACE_CACHE (it->f) == NULL)
  3292     init_frame_faces (it->f);
  3293   if (FRAME_FACE_CACHE (it->f)->used == 0)
  3294     recompute_basic_faces (it->f);
  3295 
  3296   it->override_ascent = -1;
  3297 
  3298   /* Are control characters displayed as `^C'?  */
  3299   it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
  3300 
  3301   /* -1 means everything between a CR and the following line end
  3302      is invisible.  >0 means lines indented more than this value are
  3303      invisible.  */
  3304   it->selective = (FIXNUMP (BVAR (current_buffer, selective_display))
  3305                    ? (clip_to_bounds
  3306                       (-1, XFIXNUM (BVAR (current_buffer, selective_display)),
  3307                        PTRDIFF_MAX))
  3308                    : (!NILP (BVAR (current_buffer, selective_display))
  3309                       ? -1 : 0));
  3310   it->selective_display_ellipsis_p
  3311     = !NILP (BVAR (current_buffer, selective_display_ellipses));
  3312 
  3313   /* Display table to use.  */
  3314   it->dp = window_display_table (w);
  3315 
  3316   /* Are multibyte characters enabled in current_buffer?  */
  3317   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  3318 
  3319   it->tab_width = SANE_TAB_WIDTH (current_buffer);
  3320 
  3321   /* Are lines in the display truncated?  */
  3322   if (TRUNCATE != 0)
  3323     it->line_wrap = TRUNCATE;
  3324   if (base_face_id == DEFAULT_FACE_ID
  3325       && !it->w->hscroll
  3326       && (WINDOW_FULL_WIDTH_P (it->w)
  3327           || NILP (Vtruncate_partial_width_windows)
  3328           || (FIXNUMP (Vtruncate_partial_width_windows)
  3329               /* PXW: Shall we do something about this?  */
  3330               && (XFIXNUM (Vtruncate_partial_width_windows)
  3331                   <= WINDOW_TOTAL_COLS (it->w))))
  3332       && NILP (BVAR (current_buffer, truncate_lines)))
  3333     it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
  3334       ? WINDOW_WRAP : WORD_WRAP;
  3335 
  3336   /* Get dimensions of truncation and continuation glyphs.  These are
  3337      displayed as fringe bitmaps under X, but we need them for such
  3338      frames when the fringes are turned off.  The no_special_glyphs slot
  3339      of the iterator's frame, when set, suppresses their display - by
  3340      default for tooltip frames and when set via the 'no-special-glyphs'
  3341      frame parameter.  */
  3342 #ifdef HAVE_WINDOW_SYSTEM
  3343   if (!(FRAME_WINDOW_P (it->f) && it->f->no_special_glyphs))
  3344 #endif
  3345     {
  3346       if (it->line_wrap == TRUNCATE)
  3347         {
  3348           /* We will need the truncation glyph.  */
  3349           eassert (it->glyph_row == NULL);
  3350           produce_special_glyphs (it, IT_TRUNCATION);
  3351           it->truncation_pixel_width = it->pixel_width;
  3352         }
  3353       else
  3354         {
  3355           /* We will need the continuation glyph.  */
  3356           eassert (it->glyph_row == NULL);
  3357           produce_special_glyphs (it, IT_CONTINUATION);
  3358           it->continuation_pixel_width = it->pixel_width;
  3359         }
  3360     }
  3361 
  3362   /* Reset these values to zero because the produce_special_glyphs
  3363      above has changed them.  */
  3364   it->pixel_width = it->ascent = it->descent = 0;
  3365   it->phys_ascent = it->phys_descent = 0;
  3366 
  3367   /* Set this after getting the dimensions of truncation and
  3368      continuation glyphs, so that we don't produce glyphs when calling
  3369      produce_special_glyphs, above.  */
  3370   it->glyph_row = row;
  3371   it->area = TEXT_AREA;
  3372 
  3373   /* Get the dimensions of the display area.  The display area
  3374      consists of the visible window area plus a horizontally scrolled
  3375      part to the left of the window.  All x-values are relative to the
  3376      start of this total display area.  */
  3377   if (base_face_id != DEFAULT_FACE_ID)
  3378     {
  3379       /* Mode lines, menu bar in terminal frames.  */
  3380       it->first_visible_x = 0;
  3381       it->last_visible_x =
  3382         WINDOW_PIXEL_WIDTH (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
  3383     }
  3384   else
  3385     {
  3386       /* When hscrolling only the current line, don't apply the
  3387          hscroll here, it will be applied by display_line when it gets
  3388          to laying out the line showing point.  However, if the
  3389          window's min_hscroll is positive, the user specified a lower
  3390          bound for automatic hscrolling, so they expect the
  3391          non-current lines to obey that hscroll amount.  */
  3392       if (hscrolling_current_line_p (w))
  3393         {
  3394           if (w->min_hscroll > 0)
  3395             it->first_visible_x = w->min_hscroll * FRAME_COLUMN_WIDTH (it->f);
  3396           else
  3397             it->first_visible_x = 0;
  3398         }
  3399       else
  3400         it->first_visible_x =
  3401           window_hscroll_limited (w, it->f) * FRAME_COLUMN_WIDTH (it->f);
  3402 
  3403       body_width = window_box_width (w, TEXT_AREA);
  3404       if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3405           && body_width != w->old_body_pixel_width)
  3406         FRAME_WINDOW_CHANGE (it->f) = true;
  3407       it->last_visible_x = it->first_visible_x + body_width;
  3408 
  3409       /* If we truncate lines, leave room for the truncation glyph(s) at
  3410          the right margin.  Otherwise, leave room for the continuation
  3411          glyph(s).  Done only if the window has no right fringe.  */
  3412       if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
  3413         {
  3414           if (it->line_wrap == TRUNCATE)
  3415             it->last_visible_x -= it->truncation_pixel_width;
  3416           else
  3417             it->last_visible_x -= it->continuation_pixel_width;
  3418         }
  3419 
  3420       it->tab_line_p = window_wants_tab_line (w);
  3421       it->header_line_p = window_wants_header_line (w);
  3422       body_height = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
  3423       it->current_y =  body_height + w->vscroll;
  3424     }
  3425 
  3426   /* Leave room for a border glyph.  */
  3427   if (!FRAME_WINDOW_P (it->f)
  3428       && !WINDOW_RIGHTMOST_P (it->w))
  3429     it->last_visible_x -= 1;
  3430 
  3431   it->last_visible_y = window_text_bottom_y (w);
  3432   body_height += it->last_visible_y;
  3433   if (!w->pseudo_window_p && !MINI_WINDOW_P (w)
  3434       && body_height != w->old_body_pixel_height)
  3435     FRAME_WINDOW_CHANGE (it->f) = true;
  3436 
  3437   /* For mode lines and alike, arrange for the first glyph having a
  3438      left box line if the face specifies a box.  */
  3439   if (base_face_id != DEFAULT_FACE_ID)
  3440     {
  3441       struct face *face;
  3442 
  3443       it->face_id = remapped_base_face_id;
  3444 
  3445       /* If we have a boxed mode line, make the first character appear
  3446          with a left box line.  */
  3447       face = FACE_FROM_ID_OR_NULL (it->f, remapped_base_face_id);
  3448       if (face && face->box != FACE_NO_BOX)
  3449         {
  3450           int box_thickness = face->box_vertical_line_width;
  3451           it->face_box_p = true;
  3452           it->start_of_box_run_p = true;
  3453           /* Make sure we will have enough horizontal space to add the
  3454              right box line at the end.  */
  3455           if (box_thickness > 0)
  3456             it->last_visible_x -= box_thickness;
  3457         }
  3458     }
  3459 
  3460   /* If a buffer position was specified, set the iterator there,
  3461      getting overlays and face properties from that position.  */
  3462   if (charpos >= BUF_BEG (current_buffer))
  3463     {
  3464       it->stop_charpos = charpos;
  3465       it->end_charpos = ZV;
  3466       eassert (charpos == BYTE_TO_CHAR (bytepos));
  3467       IT_CHARPOS (*it) = charpos;
  3468       IT_BYTEPOS (*it) = bytepos;
  3469 
  3470       /* We will rely on `reseat' to set this up properly, via
  3471          handle_face_prop.  */
  3472       it->face_id = it->base_face_id;
  3473 
  3474       it->start = it->current;
  3475       /* Do we need to reorder bidirectional text?  Not if this is a
  3476          unibyte buffer: by definition, none of the single-byte
  3477          characters are strong R2L, so no reordering is needed.  And
  3478          bidi.c doesn't support unibyte buffers anyway.  Also, don't
  3479          reorder while we are loading loadup.el, since the tables of
  3480          character properties needed for reordering are not yet
  3481          available.  */
  3482       it->bidi_p =
  3483         !redisplay__inhibit_bidi
  3484         && !NILP (BVAR (current_buffer, bidi_display_reordering))
  3485         && it->multibyte_p;
  3486 
  3487       /* If we are to reorder bidirectional text, init the bidi
  3488          iterator.  */
  3489       if (it->bidi_p)
  3490         {
  3491           /*  Since we don't know at this point whether there will be
  3492               any R2L lines in the window, we reserve space for
  3493               truncation/continuation glyphs even if only the left
  3494               fringe is absent.  */
  3495           if (base_face_id == DEFAULT_FACE_ID
  3496               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
  3497               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
  3498             {
  3499               if (it->line_wrap == TRUNCATE)
  3500                 it->last_visible_x -= it->truncation_pixel_width;
  3501               else
  3502                 it->last_visible_x -= it->continuation_pixel_width;
  3503             }
  3504           /* Note the paragraph direction that this buffer wants to
  3505              use.  */
  3506           if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3507                   Qleft_to_right))
  3508             it->paragraph_embedding = L2R;
  3509           else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
  3510                        Qright_to_left))
  3511             it->paragraph_embedding = R2L;
  3512           else
  3513             it->paragraph_embedding = NEUTRAL_DIR;
  3514           bidi_unshelve_cache (NULL, false);
  3515           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  3516                         &it->bidi_it);
  3517         }
  3518 
  3519       /* This is set only when long_line_optimizations_p is non-zero
  3520          for the current buffer.  */
  3521       it->medium_narrowing_begv = 0;
  3522 
  3523       /* Compute faces etc.  */
  3524       reseat (it, it->current.pos, true);
  3525     }
  3526 
  3527   CHECK_IT (it);
  3528 }
  3529 
  3530 /* How Emacs deals with long lines.
  3531 
  3532    (1) When a buffer is about to be (re)displayed, 'redisplay_window'
  3533    detects, with a heuristic, whether it contains long lines.
  3534 
  3535    This happens in 'redisplay_window' because it is only displaying
  3536    buffers with long lines that is problematic.  In other words, none
  3537    of the optimizations described below is ever used in buffers that
  3538    are never displayed.
  3539 
  3540    This happens with a heuristic, which checks whether a buffer
  3541    contains long lines, each time its contents have changed "enough"
  3542    between two redisplay cycles, because a buffer without long lines
  3543    can become a buffer with long lines at any time, for example after
  3544    a yank command, or after a replace command, or while the output of
  3545    an external process is inserted in a buffer.
  3546 
  3547    When Emacs has detected that a buffer contains long lines, the
  3548    buffer-local variable 'long_line_optimizations_p' (in 'struct
  3549    buffer') is set, and Emacs does not try to detect whether the
  3550    buffer does or does not contain long lines anymore.
  3551 
  3552    What a long line is depends on the variable 'long-line-threshold',
  3553    whose default value is 50000 (characters).
  3554 
  3555    (2) When a buffer with long lines is (re)displayed, the amount of
  3556    data that the display routines consider is, in a few well-chosen
  3557    places, limited with a temporary restriction, whose bounds are
  3558    calculated with the functions below.
  3559 
  3560    (2.1) 'get_small_narrowing_begv' is used to create a restriction
  3561    which starts a few hundred characters before point.  The exact
  3562    number of characters depends on the width of the window in which
  3563    the buffer is displayed.
  3564 
  3565    There is no corresponding 'get_small_narrowing_zv' function,
  3566    because it is not necessary to set the end limit of that
  3567    restriction.
  3568 
  3569    This restriction is used in four places, namely:
  3570    'back_to_previous_line_start' and 'move_it_vertically_backward'
  3571    (with the 'SET_WITH_NARROWED_BEGV' macro), and in
  3572    'composition_compute_stop_pos' and 'find_automatic_composition' (in
  3573    a conditional statement depending on 'long_line_optimizations_p').
  3574 
  3575    (2.2) 'get_medium_narrowing_begv' is used to create a restriction
  3576    which starts a few thousand characters before point.  The exact
  3577    number of characters depends on the size (width and height) of the
  3578    window in which the buffer is displayed.  For performance reasons,
  3579    the return value of that function is cached in 'struct it', in the
  3580    'medium_narrowing_begv' field.
  3581 
  3582    The corresponding function 'get_medium_narrowing_zv' (and
  3583    'medium_narrowing_zv' field in 'struct it') is not used to set the
  3584    end limit of the restriction, which is again unnecessary, but to
  3585    determine, in 'reseat', whether the iterator has moved far enough
  3586    from its original position, and whether the start position of the
  3587    restriction must be computed anew.
  3588 
  3589    This restriction is used in a single place:
  3590    'get_visually_first_element', with the 'SET_WITH_NARROWED_BEGV'
  3591    macro.
  3592 
  3593    (2.3) 'get_large_narrowing_begv' and 'get_large_narrowing_zv' are
  3594    used to create a restriction which starts a few hundred thousand
  3595    characters before point and ends a few hundred thousand characters
  3596    after point.  The size of that restriction depends on the variable
  3597    'long-line-optimizations-region-size', whose default value is
  3598    500000 (characters); it can be adjusted by a few hundred characters
  3599    depending on 'long-line-optimizations-bol-search-limit', whose
  3600    default value is 128 (characters).
  3601 
  3602    For performance reasons again, the return values of these functions
  3603    are stored in the 'large_narrowing_begv' and 'large_narrowing_zv'
  3604    fields in 'struct it'.
  3605 
  3606    The restriction defined by these values is used around three
  3607    low-level hooks: around 'fontification-functions', in
  3608    'handle_fontified_prop', and around 'pre-command-hook' and
  3609    'post-command-hook', in 'safe_run_hooks_maybe_narrowed', which is
  3610    called in 'command_loop_1'.  These restrictions are set around
  3611    these hooks with 'labeled_narrow_to_region'; the restrictions are
  3612    labeled, and cannot be removed with a call to 'widen', but can be
  3613    removed with 'without-restriction' with a :label argument.
  3614 */
  3615 
  3616 static int
  3617 get_narrowed_width (struct window *w)
  3618 {
  3619   /* In a character-only terminal, only one font size is used, so we
  3620      can use a smaller factor.  */
  3621   int fact = FRAME_WINDOW_P (XFRAME (w->frame)) ? 3 : 2;
  3622   /* If the window has no fringes (in a character-only terminal or in
  3623      a GUI frame without fringes), subtract 1 from the width for the
  3624      '\' line wrapping character.  */
  3625   int width = window_body_width (w, WINDOW_BODY_IN_CANONICAL_CHARS)
  3626     - ((WINDOW_RIGHT_FRINGE_WIDTH (w) == 0
  3627         || WINDOW_LEFT_FRINGE_WIDTH (w) == 0) ? 1 : 0);
  3628   return fact * max (1, width);
  3629 }
  3630 
  3631 static int
  3632 get_narrowed_len (struct window *w)
  3633 {
  3634   int height = window_body_height (w, WINDOW_BODY_IN_CANONICAL_CHARS);
  3635   return get_narrowed_width (w) * max (1, height);
  3636 }
  3637 
  3638 static ptrdiff_t
  3639 get_medium_narrowing_begv (struct window *w, ptrdiff_t pos)
  3640 {
  3641   int len = get_narrowed_len (w);
  3642   return max ((pos / len - 1) * len, BEGV);
  3643 }
  3644 
  3645 static ptrdiff_t
  3646 get_medium_narrowing_zv (struct window *w, ptrdiff_t pos)
  3647 {
  3648   int len = get_narrowed_len (w);
  3649   return min ((pos / len + 1) * len, ZV);
  3650 }
  3651 
  3652 /* Find the position of the last BOL before POS, unless it is too far
  3653    away.  The buffer portion in which the search occurs is gradually
  3654    enlarged: [POS-500..POS], [POS-5500..POS-500],
  3655    [POS-55500..POS-5500], and finally [POS-555500..POS-55500].  Return
  3656    BEGV-1 if no BOL was found in [POS-555500..POS].  */
  3657 static ptrdiff_t
  3658 get_nearby_bol_pos (ptrdiff_t pos)
  3659 {
  3660   ptrdiff_t start, pos_bytepos, cur, next, found, bol = BEGV - 1, init_pos = pos;
  3661   int dist;
  3662   for (dist = 500; dist <= 500000; dist *= 10)
  3663     {
  3664       pos_bytepos = pos == BEGV ? BEGV_BYTE : CHAR_TO_BYTE (pos);
  3665       start = pos - dist < BEGV ? BEGV : pos - dist;
  3666       for (cur = start; cur < pos; cur = next)
  3667         {
  3668           next = find_newline1 (cur, CHAR_TO_BYTE (cur),
  3669                                 pos, pos_bytepos,
  3670                                 1, &found, NULL, false);
  3671           if (found)
  3672             bol = next;
  3673           else
  3674             break;
  3675         }
  3676       if (bol >= BEGV || start == BEGV)
  3677         break;
  3678       else
  3679         pos = pos - dist < BEGV ? BEGV : pos - dist;
  3680     }
  3681   eassert (bol <= init_pos);
  3682   return bol;
  3683 }
  3684 
  3685 ptrdiff_t
  3686 get_small_narrowing_begv (struct window *w, ptrdiff_t pos)
  3687 {
  3688   int len = get_narrowed_width (w);
  3689   ptrdiff_t bol_pos = max (get_nearby_bol_pos (pos), BEGV);
  3690   return max (bol_pos + ((pos - bol_pos) / len - 1) * len, BEGV);
  3691 }
  3692 
  3693 ptrdiff_t
  3694 get_large_narrowing_begv (ptrdiff_t pos)
  3695 {
  3696   if (long_line_optimizations_region_size <= 0)
  3697     return BEGV;
  3698   int len = long_line_optimizations_region_size / 2;
  3699   int begv = max (pos - len, BEGV);
  3700   int limit = long_line_optimizations_bol_search_limit;
  3701   while (limit > 0)
  3702     {
  3703       if (begv == BEGV || FETCH_BYTE (CHAR_TO_BYTE (begv) - 1) == '\n')
  3704         return begv;
  3705       begv--;
  3706       limit--;
  3707     }
  3708   return begv;
  3709 }
  3710 
  3711 ptrdiff_t
  3712 get_large_narrowing_zv (ptrdiff_t pos)
  3713 {
  3714   if (long_line_optimizations_region_size <= 0)
  3715     return ZV;
  3716   int len = long_line_optimizations_region_size / 2;
  3717   return min (pos + len, ZV);
  3718 }
  3719 
  3720 static void
  3721 unwind_narrowed_begv (Lisp_Object point_min)
  3722 {
  3723   SET_BUF_BEGV (current_buffer, XFIXNUM (point_min));
  3724 }
  3725 
  3726 /* Set DST to EXPR.  When IT indicates that BEGV should temporarily be
  3727    updated to optimize display, evaluate EXPR with BEGV set to BV.  */
  3728 
  3729 #define SET_WITH_NARROWED_BEGV(IT,DST,EXPR,BV)                          \
  3730   do {                                                                  \
  3731     if (IT->medium_narrowing_begv)                                      \
  3732       {                                                                 \
  3733         specpdl_ref count = SPECPDL_INDEX ();                           \
  3734         record_unwind_protect (unwind_narrowed_begv, Fpoint_min ());    \
  3735         SET_BUF_BEGV (current_buffer, BV);                              \
  3736         DST = EXPR;                                                     \
  3737         unbind_to (count, Qnil);                                        \
  3738       }                                                                 \
  3739     else                                                                \
  3740       DST = EXPR;                                                       \
  3741   } while (0)
  3742 
  3743 /* Initialize IT for the display of window W with window start POS.  */
  3744 
  3745 void
  3746 start_display (struct it *it, struct window *w, struct text_pos pos)
  3747 {
  3748   struct glyph_row *row;
  3749   int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
  3750 
  3751   row = w->desired_matrix->rows + first_vpos;
  3752   init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
  3753   it->first_vpos = first_vpos;
  3754 
  3755   /* Don't reseat to previous visible line start if current start
  3756      position is in a string or image.  */
  3757   if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
  3758     {
  3759       int first_y = it->current_y;
  3760 
  3761       /* If window start is not at a line start, skip forward to POS to
  3762          get the correct continuation lines width.  */
  3763       bool start_at_line_beg_p = (CHARPOS (pos) == BEGV
  3764                                   || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
  3765       if (!start_at_line_beg_p)
  3766         {
  3767           int new_x;
  3768 
  3769           reseat_at_previous_visible_line_start (it);
  3770           move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
  3771 
  3772           new_x = it->current_x + it->pixel_width;
  3773 
  3774           /* If lines are continued, this line may end in the middle
  3775              of a multi-glyph character (e.g. a control character
  3776              displayed as \003, or in the middle of an overlay
  3777              string).  In this case move_it_to above will not have
  3778              taken us to the start of the continuation line but to the
  3779              end of the continued line.  */
  3780           if (it->current_x > 0
  3781               && it->line_wrap != TRUNCATE /* Lines are continued.  */
  3782               && (/* And glyph doesn't fit on the line.  */
  3783                   new_x > it->last_visible_x
  3784                   /* Or it fits exactly and we're on a window
  3785                      system frame.  */
  3786                   || (new_x == it->last_visible_x
  3787                       && FRAME_WINDOW_P (it->f)
  3788                       && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  3789                           ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
  3790                           : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
  3791             {
  3792               if ((it->current.dpvec_index >= 0
  3793                    || it->current.overlay_string_index >= 0)
  3794                   /* If we are on a newline from a display vector or
  3795                      overlay string, then we are already at the end of
  3796                      a screen line; no need to go to the next line in
  3797                      that case, as this line is not really continued.
  3798                      (If we do go to the next line, C-e will not DTRT.)  */
  3799                   && it->c != '\n')
  3800                 {
  3801                   set_iterator_to_next (it, true);
  3802                   move_it_in_display_line_to (it, -1, -1, 0);
  3803                 }
  3804 
  3805               it->continuation_lines_width += it->current_x;
  3806             }
  3807           /* If the character at POS is displayed via a display
  3808              vector, move_it_to above stops at the final glyph of
  3809              IT->dpvec.  To make the caller redisplay that character
  3810              again (a.k.a. start at POS), we need to reset the
  3811              dpvec_index to the beginning of IT->dpvec.  */
  3812           else if (it->current.dpvec_index >= 0)
  3813             it->current.dpvec_index = 0;
  3814 
  3815           /* We're starting a new display line, not affected by the
  3816              height of the continued line, so clear the appropriate
  3817              fields in the iterator structure.  */
  3818           it->max_ascent = it->max_descent = 0;
  3819           it->max_phys_ascent = it->max_phys_descent = 0;
  3820 
  3821           it->current_y = first_y;
  3822           it->vpos = 0;
  3823           it->current_x = it->hpos = 0;
  3824         }
  3825     }
  3826 }
  3827 
  3828 
  3829 /* Return true if POS is a position in ellipses displayed for invisible
  3830    text.  W is the window we display, for text property lookup.  */
  3831 
  3832 static bool
  3833 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
  3834 {
  3835   Lisp_Object prop, window;
  3836   bool ellipses_p = false;
  3837   ptrdiff_t charpos = CHARPOS (pos->pos);
  3838 
  3839   /* If POS specifies a position in a display vector, this might
  3840      be for an ellipsis displayed for invisible text.  We won't
  3841      get the iterator set up for delivering that ellipsis unless
  3842      we make sure that it gets aware of the invisible text.  */
  3843   if (pos->dpvec_index >= 0
  3844       && pos->overlay_string_index < 0
  3845       && CHARPOS (pos->string_pos) < 0
  3846       && charpos > BEGV
  3847       && (XSETWINDOW (window, w),
  3848           prop = Fget_char_property (make_fixnum (charpos),
  3849                                      Qinvisible, window),
  3850           TEXT_PROP_MEANS_INVISIBLE (prop) == 0))
  3851     {
  3852       prop = Fget_char_property (make_fixnum (charpos - 1), Qinvisible,
  3853                                  window);
  3854       ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
  3855     }
  3856 
  3857   return ellipses_p;
  3858 }
  3859 
  3860 
  3861 /* Initialize IT for stepping through current_buffer in window W,
  3862    starting at position POS that includes overlay string and display
  3863    vector/ control character translation position information.  Value
  3864    is false if there are overlay strings with newlines at POS.  */
  3865 
  3866 static bool
  3867 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
  3868 {
  3869   ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
  3870   int i;
  3871   bool overlay_strings_with_newlines = false;
  3872 
  3873   /* If POS specifies a position in a display vector, this might
  3874      be for an ellipsis displayed for invisible text.  We won't
  3875      get the iterator set up for delivering that ellipsis unless
  3876      we make sure that it gets aware of the invisible text.  */
  3877   if (in_ellipses_for_invisible_text_p (pos, w))
  3878     {
  3879       --charpos;
  3880       bytepos = 0;
  3881     }
  3882 
  3883   /* Keep in mind: the call to reseat in init_iterator skips invisible
  3884      text, so we might end up at a position different from POS.  This
  3885      is only a problem when POS is a row start after a newline and an
  3886      overlay starts there with an after-string, and the overlay has an
  3887      invisible property.  Since we don't skip invisible text in
  3888      display_line and elsewhere immediately after consuming the
  3889      newline before the row start, such a POS will not be in a string,
  3890      but the call to init_iterator below will move us to the
  3891      after-string.  */
  3892   init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
  3893 
  3894   /* This only scans the current chunk -- it should scan all chunks.
  3895      However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
  3896      to 16 in 22.1 to make this a lesser problem.  */
  3897   for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
  3898     {
  3899       const char *s = SSDATA (it->overlay_strings[i]);
  3900       const char *e = s + SBYTES (it->overlay_strings[i]);
  3901 
  3902       while (s < e && *s != '\n')
  3903         ++s;
  3904 
  3905       if (s < e)
  3906         {
  3907           overlay_strings_with_newlines = true;
  3908           break;
  3909         }
  3910     }
  3911 
  3912   /* If position is within an overlay string, set up IT to the right
  3913      overlay string.  */
  3914   if (pos->overlay_string_index >= 0)
  3915     {
  3916       int relative_index;
  3917 
  3918       /* If the first overlay string happens to have a `display'
  3919          property for an image, the iterator will be set up for that
  3920          image, and we have to undo that setup first before we can
  3921          correct the overlay string index.  */
  3922       if (it->method == GET_FROM_IMAGE)
  3923         pop_it (it);
  3924 
  3925       /* We already have the first chunk of overlay strings in
  3926          IT->overlay_strings.  Load more until the one for
  3927          pos->overlay_string_index is in IT->overlay_strings.  */
  3928       if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
  3929         {
  3930           ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
  3931           it->current.overlay_string_index = 0;
  3932           while (n--)
  3933             {
  3934               load_overlay_strings (it, 0);
  3935               it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
  3936             }
  3937         }
  3938 
  3939       it->current.overlay_string_index = pos->overlay_string_index;
  3940       relative_index = (it->current.overlay_string_index
  3941                         % OVERLAY_STRING_CHUNK_SIZE);
  3942       it->string = it->overlay_strings[relative_index];
  3943       eassert (STRINGP (it->string));
  3944       it->current.string_pos = pos->string_pos;
  3945       it->method = GET_FROM_STRING;
  3946       it->end_charpos = SCHARS (it->string);
  3947       /* Set up the bidi iterator for this overlay string.  */
  3948       if (it->bidi_p)
  3949         {
  3950           it->bidi_it.string.lstring = it->string;
  3951           it->bidi_it.string.s = NULL;
  3952           it->bidi_it.string.schars = SCHARS (it->string);
  3953           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  3954           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  3955           it->bidi_it.string.unibyte = !it->multibyte_p;
  3956           it->bidi_it.w = it->w;
  3957           bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3958                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  3959 
  3960           /* Synchronize the state of the bidi iterator with
  3961              pos->string_pos.  For any string position other than
  3962              zero, this will be done automagically when we resume
  3963              iteration over the string and get_visually_first_element
  3964              is called.  But if string_pos is zero, and the string is
  3965              to be reordered for display, we need to resync manually,
  3966              since it could be that the iteration state recorded in
  3967              pos ended at string_pos of 0 moving backwards in string.  */
  3968           if (CHARPOS (pos->string_pos) == 0)
  3969             {
  3970               get_visually_first_element (it);
  3971               if (IT_STRING_CHARPOS (*it) != 0)
  3972                 do {
  3973                   /* Paranoia.  */
  3974                   eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
  3975                   bidi_move_to_visually_next (&it->bidi_it);
  3976                 } while (it->bidi_it.charpos != 0);
  3977             }
  3978           eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  3979                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
  3980         }
  3981     }
  3982 
  3983   if (CHARPOS (pos->string_pos) >= 0)
  3984     {
  3985       /* Recorded position is not in an overlay string, but in another
  3986          string.  This can only be a string from a `display' property.
  3987          IT should already be filled with that string.  */
  3988       it->current.string_pos = pos->string_pos;
  3989       eassert (STRINGP (it->string));
  3990       if (it->bidi_p)
  3991         bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
  3992                       FRAME_WINDOW_P (it->f), &it->bidi_it);
  3993     }
  3994 
  3995   /* Restore position in display vector translations, control
  3996      character translations or ellipses.  */
  3997   if (pos->dpvec_index >= 0)
  3998     {
  3999       if (it->dpvec == NULL)
  4000         get_next_display_element (it);
  4001       eassert (it->dpvec && it->current.dpvec_index == 0);
  4002       it->current.dpvec_index = pos->dpvec_index;
  4003     }
  4004 
  4005   CHECK_IT (it);
  4006   return !overlay_strings_with_newlines;
  4007 }
  4008 
  4009 
  4010 /* Initialize IT for stepping through current_buffer in window W
  4011    starting at ROW->start.  */
  4012 
  4013 static void
  4014 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
  4015 {
  4016   init_from_display_pos (it, w, &row->start);
  4017   it->start = row->start;
  4018   it->continuation_lines_width = row->continuation_lines_width;
  4019   CHECK_IT (it);
  4020 }
  4021 
  4022 
  4023 /* Initialize IT for stepping through current_buffer in window W
  4024    starting in the line following ROW, i.e. starting at ROW->end.
  4025    Value is false if there are overlay strings with newlines at ROW's
  4026    end position, or if the following row begins with bidi-reordered
  4027    characters that could be composed.  */
  4028 
  4029 static bool
  4030 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
  4031 {
  4032   bool success = false;
  4033 
  4034   if (init_from_display_pos (it, w, &row->end))
  4035     {
  4036       if (row->continued_p)
  4037         it->continuation_lines_width
  4038           = row->continuation_lines_width + row->pixel_width;
  4039       CHECK_IT (it);
  4040       /* Initializing IT in the presence of compositions in reordered
  4041          rows is tricky: row->end above will generally cause us to
  4042          start at position that is not the first one in the logical
  4043          order, and we might therefore miss the composition earlier in
  4044          the buffer that affects how glypsh are laid out in this row.
  4045          So we punt instead.  Note: the test below works because
  4046          get_next_display_element calls get_visually_first_element,
  4047          which calls composition_compute_stop_pos, which populates
  4048          it->cmp_it.  */
  4049       if (get_next_display_element (it)
  4050           && (it->bidi_it.scan_dir == -1 && it->cmp_it.id >= 0))
  4051         success = false;
  4052       else
  4053         success = true;
  4054     }
  4055 
  4056   return success;
  4057 }
  4058 
  4059 
  4060 
  4061 
  4062 /***********************************************************************
  4063                            Text properties
  4064  ***********************************************************************/
  4065 
  4066 /* Called when IT reaches IT->stop_charpos.  Handle text property and
  4067    overlay changes.  Set IT->stop_charpos to the next position where
  4068    to stop.  */
  4069 
  4070 static void
  4071 handle_stop (struct it *it)
  4072 {
  4073   enum prop_handled handled;
  4074   bool handle_overlay_change_p;
  4075   struct props *p;
  4076 
  4077   it->dpvec = NULL;
  4078   it->current.dpvec_index = -1;
  4079   handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
  4080   it->ellipsis_p = false;
  4081 
  4082   /* Use face of preceding text for ellipsis (if invisible) */
  4083   if (it->selective_display_ellipsis_p)
  4084     it->saved_face_id = it->face_id;
  4085 
  4086   /* Here's the description of the semantics of, and the logic behind,
  4087      the various HANDLED_* statuses:
  4088 
  4089      HANDLED_NORMALLY means the handler did its job, and the loop
  4090      should proceed to calling the next handler in order.
  4091 
  4092      HANDLED_RECOMPUTE_PROPS means the handler caused a significant
  4093      change in the properties and overlays at current position, so the
  4094      loop should be restarted, to re-invoke the handlers that were
  4095      already called.  This happens when fontification-functions were
  4096      called by handle_fontified_prop, and actually fontified
  4097      something.  Another case where HANDLED_RECOMPUTE_PROPS is
  4098      returned is when we discover overlay strings that need to be
  4099      displayed right away.  The loop below will continue for as long
  4100      as the status is HANDLED_RECOMPUTE_PROPS.
  4101 
  4102      HANDLED_RETURN means return immediately to the caller, to
  4103      continue iteration without calling any further handlers.  This is
  4104      used when we need to act on some property right away, for example
  4105      when we need to display the ellipsis or a replacing display
  4106      property, such as display string or image.
  4107 
  4108      HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
  4109      consumed, and the handler switched to the next overlay string.
  4110      This signals the loop below to refrain from looking for more
  4111      overlays before all the overlay strings of the current overlay
  4112      are processed.
  4113 
  4114      Some of the handlers called by the loop push the iterator state
  4115      onto the stack (see 'push_it'), and arrange for the iteration to
  4116      continue with another object, such as an image, a display string,
  4117      or an overlay string.  In most such cases, it->stop_charpos is
  4118      set to the first character of the string, so that when the
  4119      iteration resumes, this function will immediately be called
  4120      again, to examine the properties at the beginning of the string.
  4121 
  4122      When a display or overlay string is exhausted, the iterator state
  4123      is popped (see 'pop_it'), and iteration continues with the
  4124      previous object.  Again, in many such cases this function is
  4125      called again to find the next position where properties might
  4126      change.  */
  4127 
  4128   do
  4129     {
  4130       handled = HANDLED_NORMALLY;
  4131 
  4132       /* Call text property handlers.  */
  4133       for (p = it_props; p->handler; ++p)
  4134         {
  4135           handled = p->handler (it);
  4136 
  4137           if (handled == HANDLED_RECOMPUTE_PROPS)
  4138             break;
  4139           else if (handled == HANDLED_RETURN)
  4140             {
  4141               /* We still want to show before and after strings from
  4142                  overlays even if the actual buffer text is replaced.  */
  4143               if (!handle_overlay_change_p
  4144                   || it->sp > 1
  4145                   /* Don't call get_overlay_strings_1 if we already
  4146                      have overlay strings loaded, because doing so
  4147                      will load them again and push the iterator state
  4148                      onto the stack one more time, which is not
  4149                      expected by the rest of the code that processes
  4150                      overlay strings.  */
  4151                   || (it->current.overlay_string_index < 0
  4152                       && !get_overlay_strings_1 (it, 0, false)))
  4153                 {
  4154                   if (it->ellipsis_p)
  4155                     setup_for_ellipsis (it, 0);
  4156                   /* When handling a display spec, we might load an
  4157                      empty string.  In that case, discard it here.  We
  4158                      used to discard it in handle_single_display_spec,
  4159                      but that causes get_overlay_strings_1, above, to
  4160                      ignore overlay strings that we must check.  */
  4161                   if (STRINGP (it->string) && !SCHARS (it->string))
  4162                     pop_it (it);
  4163                   return;
  4164                 }
  4165               else if (STRINGP (it->string) && !SCHARS (it->string))
  4166                 pop_it (it);
  4167               else
  4168                 {
  4169                   it->string_from_display_prop_p = false;
  4170                   it->from_disp_prop_p = false;
  4171                   handle_overlay_change_p = false;
  4172                 }
  4173               handled = HANDLED_RECOMPUTE_PROPS;
  4174               break;
  4175             }
  4176           else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
  4177             handle_overlay_change_p = false;
  4178         }
  4179 
  4180       if (handled != HANDLED_RECOMPUTE_PROPS)
  4181         {
  4182           /* Don't check for overlay strings below when set to deliver
  4183              characters from a display vector.  */
  4184           if (it->method == GET_FROM_DISPLAY_VECTOR)
  4185             handle_overlay_change_p = false;
  4186 
  4187           /* Handle overlay changes.
  4188              This sets HANDLED to HANDLED_RECOMPUTE_PROPS
  4189              if it finds overlays.  */
  4190           if (handle_overlay_change_p)
  4191             handled = handle_overlay_change (it);
  4192         }
  4193 
  4194       if (it->ellipsis_p)
  4195         {
  4196           setup_for_ellipsis (it, 0);
  4197           break;
  4198         }
  4199     }
  4200   while (handled == HANDLED_RECOMPUTE_PROPS);
  4201 
  4202   /* Determine where to stop next.  */
  4203   if (handled == HANDLED_NORMALLY)
  4204     compute_stop_pos (it);
  4205 }
  4206 
  4207 
  4208 /* Compute IT->stop_charpos from text property and overlay change
  4209    information for IT's current position.  */
  4210 
  4211 static void
  4212 compute_stop_pos (struct it *it)
  4213 {
  4214   register INTERVAL iv, next_iv;
  4215   Lisp_Object object, limit, position;
  4216   ptrdiff_t charpos, bytepos, cmp_limit_pos = -1;
  4217 
  4218   if (STRINGP (it->string))
  4219     {
  4220       /* Strings are usually short, so don't limit the search for
  4221          properties.  */
  4222       it->stop_charpos = it->end_charpos;
  4223       object = it->string;
  4224       limit = Qnil;
  4225       charpos = IT_STRING_CHARPOS (*it);
  4226       bytepos = IT_STRING_BYTEPOS (*it);
  4227     }
  4228   else
  4229     {
  4230       ptrdiff_t pos;
  4231 
  4232       /* If end_charpos is out of range for some reason, such as a
  4233          misbehaving display function, rationalize it (Bug#5984).  */
  4234       if (it->end_charpos > ZV)
  4235         it->end_charpos = ZV;
  4236       it->stop_charpos = it->end_charpos;
  4237 
  4238       /* If next overlay change is in front of the current stop pos
  4239          (which is IT->end_charpos), stop there.  Note: value of
  4240          next_overlay_change is point-max if no overlay change
  4241          follows.  */
  4242       charpos = IT_CHARPOS (*it);
  4243       bytepos = IT_BYTEPOS (*it);
  4244       pos = next_overlay_change (charpos);
  4245       if (pos < it->stop_charpos)
  4246         it->stop_charpos = pos;
  4247       /* If we are breaking compositions at point, stop at point.  */
  4248       if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
  4249           && !NILP (Vauto_composition_mode)
  4250           && composition_break_at_point
  4251           && charpos < PT && PT < it->stop_charpos)
  4252         it->stop_charpos = PT;
  4253 
  4254       /* Set up variables for computing the stop position from text
  4255          property changes.  */
  4256       XSETBUFFER (object, current_buffer);
  4257       pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4258       /* Make sure the above arbitrary limit position is not in the
  4259          middle of composable text, so we don't break compositions by
  4260          submitting the composable text to the shaper in separate
  4261          chunks.  We play safe here by assuming that only SPC, TAB,
  4262          FF, and NL cannot be in some composition; in particular, most
  4263          ASCII punctuation characters could be composed into ligatures.  */
  4264       if (!composition_break_at_point
  4265           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  4266           && !NILP (Vauto_composition_mode))
  4267         {
  4268           ptrdiff_t endpos = charpos + 10 * TEXT_PROP_DISTANCE_LIMIT;
  4269           bool found = false;
  4270 
  4271           if (pos > ZV)
  4272             pos = ZV;
  4273           if (endpos > ZV)
  4274             endpos = ZV;
  4275           ptrdiff_t bpos = CHAR_TO_BYTE (pos);
  4276           while (pos < endpos)
  4277             {
  4278               int ch = fetch_char_advance_no_check (&pos, &bpos);
  4279               if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\f')
  4280                 {
  4281                   found = true;
  4282                   break;
  4283                 }
  4284             }
  4285           if (found)
  4286             {
  4287               pos--;
  4288               cmp_limit_pos = pos;
  4289             }
  4290           else if (it->stop_charpos < endpos)
  4291             pos = it->stop_charpos;
  4292           else
  4293             {
  4294               /* Give up and use the original arbitrary limit.  */
  4295               pos = charpos + TEXT_PROP_DISTANCE_LIMIT;
  4296             }
  4297         }
  4298       limit = make_fixnum (pos);
  4299     }
  4300 
  4301   /* Get the interval containing IT's position.  Value is a null
  4302      interval if there isn't such an interval.  */
  4303   position = make_fixnum (charpos);
  4304   iv = validate_interval_range (object, &position, &position, false);
  4305   if (iv)
  4306     {
  4307       Lisp_Object values_here[LAST_PROP_IDX];
  4308       struct props *p;
  4309 
  4310       /* Get properties here.  */
  4311       for (p = it_props; p->handler; ++p)
  4312         values_here[p->idx] = textget (iv->plist,
  4313                                        builtin_lisp_symbol (p->name));
  4314 
  4315       /* Look for an interval following iv that has different
  4316          properties.  */
  4317       for (next_iv = next_interval (iv);
  4318            (next_iv
  4319             && (NILP (limit)
  4320                 || XFIXNAT (limit) > next_iv->position));
  4321            next_iv = next_interval (next_iv))
  4322         {
  4323           for (p = it_props; p->handler; ++p)
  4324             {
  4325               Lisp_Object new_value = textget (next_iv->plist,
  4326                                                builtin_lisp_symbol (p->name));
  4327               if (!EQ (values_here[p->idx], new_value))
  4328                 break;
  4329             }
  4330 
  4331           if (p->handler)
  4332             break;
  4333         }
  4334 
  4335       if (next_iv)
  4336         {
  4337           if (FIXNUMP (limit)
  4338               && next_iv->position >= XFIXNAT (limit))
  4339             /* No text property change up to limit.  */
  4340             it->stop_charpos = min (XFIXNAT (limit), it->stop_charpos);
  4341           else
  4342             /* Text properties change in next_iv.  */
  4343             it->stop_charpos = min (it->stop_charpos, next_iv->position);
  4344         }
  4345     }
  4346 
  4347   if (it->cmp_it.id < 0
  4348       && (STRINGP (it->string)
  4349           || ((!it->bidi_p || it->bidi_it.scan_dir >= 0)
  4350               && it->cmp_it.stop_pos <= IT_CHARPOS (*it))))
  4351     {
  4352       ptrdiff_t stoppos = it->end_charpos;
  4353 
  4354       /* If we found, above, a buffer position that cannot be part of
  4355          an automatic composition, limit the search of composable
  4356          characters to that position.  */
  4357       if (it->bidi_p && it->bidi_it.scan_dir < 0)
  4358         stoppos = -1;
  4359       else if (cmp_limit_pos > 0)
  4360         stoppos = cmp_limit_pos;
  4361       /* Force composition_compute_stop_pos avoid the costly search
  4362          for static compositions, since those were already found by
  4363          looking at text properties, above.  */
  4364       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
  4365                                     stoppos, it->string, false);
  4366     }
  4367 
  4368   eassert (STRINGP (it->string)
  4369            || (it->stop_charpos >= BEGV
  4370                && it->stop_charpos >= IT_CHARPOS (*it)));
  4371 }
  4372 
  4373 /* How many characters forward to search for a display property or
  4374    display string.  Searching too far forward makes the bidi display
  4375    sluggish, especially in small windows.  */
  4376 #define MAX_DISP_SCAN 250
  4377 
  4378 /* Return the character position of a display string at or after
  4379    position specified by POSITION.  If no display string exists at or
  4380    after POSITION, return ZV.  A display string is either an overlay
  4381    with `display' property whose value is a string, or a `display'
  4382    text property whose value is a string.  STRING is data about the
  4383    string to iterate; if STRING->lstring is nil, we are iterating a
  4384    buffer.  FRAME_WINDOW_P is true when we are displaying a window
  4385    on a GUI frame.  DISP_PROP is set to zero if we searched
  4386    MAX_DISP_SCAN characters forward without finding any display
  4387    strings, non-zero otherwise.  It is set to 2 if the display string
  4388    uses any kind of `(space ...)' spec that will produce a stretch of
  4389    white space in the text area.  */
  4390 ptrdiff_t
  4391 compute_display_string_pos (struct text_pos *position,
  4392                             struct bidi_string_data *string,
  4393                             struct window *w,
  4394                             bool frame_window_p, int *disp_prop)
  4395 {
  4396   /* OBJECT = nil means current buffer.  */
  4397   Lisp_Object object, object1;
  4398   Lisp_Object pos, spec, limpos;
  4399   bool string_p = string && (STRINGP (string->lstring) || string->s);
  4400   ptrdiff_t eob = string_p ? string->schars : ZV;
  4401   ptrdiff_t begb = string_p ? 0 : BEGV;
  4402   ptrdiff_t bufpos, charpos = CHARPOS (*position);
  4403   ptrdiff_t lim =
  4404     (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
  4405   struct text_pos tpos;
  4406   int rv = 0;
  4407 
  4408   if (string && STRINGP (string->lstring))
  4409     object1 = object = string->lstring;
  4410   else if (w && !string_p)
  4411     {
  4412       XSETWINDOW (object, w);
  4413       object1 = Qnil;
  4414     }
  4415   else
  4416     object1 = object = Qnil;
  4417 
  4418   *disp_prop = 1;
  4419 
  4420   if (charpos >= eob
  4421       /* We don't support display properties whose values are strings
  4422          that have display string properties.  */
  4423       || string->from_disp_str
  4424       /* C strings cannot have display properties.  */
  4425       || (string->s && !STRINGP (object)))
  4426     {
  4427       *disp_prop = 0;
  4428       return eob;
  4429     }
  4430 
  4431   /* If the character at CHARPOS is where the display string begins,
  4432      return CHARPOS.  */
  4433   pos = make_fixnum (charpos);
  4434   if (STRINGP (object))
  4435     bufpos = string->bufpos;
  4436   else
  4437     bufpos = charpos;
  4438   tpos = *position;
  4439   if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
  4440       && (charpos <= begb
  4441           || !EQ (Fget_char_property (make_fixnum (charpos - 1), Qdisplay,
  4442                                       object),
  4443                   spec))
  4444       && (rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos, bufpos,
  4445                                     frame_window_p)))
  4446     {
  4447       if (rv == 2)
  4448         *disp_prop = 2;
  4449       return charpos;
  4450     }
  4451 
  4452   /* Look forward for the first character with a `display' property
  4453      that will replace the underlying text when displayed.  */
  4454   limpos = make_fixnum (lim);
  4455   do {
  4456     pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
  4457     CHARPOS (tpos) = XFIXNAT (pos);
  4458     if (CHARPOS (tpos) >= lim)
  4459       {
  4460         *disp_prop = 0;
  4461         break;
  4462       }
  4463     if (STRINGP (object))
  4464       BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
  4465     else
  4466       BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
  4467     spec = Fget_char_property (pos, Qdisplay, object);
  4468     if (!STRINGP (object))
  4469       bufpos = CHARPOS (tpos);
  4470   } while (NILP (spec)
  4471            || !(rv = handle_display_spec (NULL, spec, object1, Qnil, &tpos,
  4472                                           bufpos, frame_window_p)));
  4473   if (rv == 2)
  4474     *disp_prop = 2;
  4475 
  4476   return CHARPOS (tpos);
  4477 }
  4478 
  4479 /* Return the character position of the end of the display string that
  4480    started at CHARPOS.  If there's no display string at CHARPOS,
  4481    return -1.  A display string is either an overlay with `display'
  4482    property whose value is a string or a `display' text property whose
  4483    value is a string.  */
  4484 ptrdiff_t
  4485 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
  4486 {
  4487   /* OBJECT = nil means current buffer.  */
  4488   Lisp_Object object =
  4489     (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
  4490   Lisp_Object pos = make_fixnum (charpos);
  4491   ptrdiff_t eob =
  4492     (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
  4493 
  4494   if (charpos >= eob || (string->s && !STRINGP (object)))
  4495     return eob;
  4496 
  4497   /* It could happen that the display property or overlay was removed
  4498      since we found it in compute_display_string_pos above.  One way
  4499      this can happen is if JIT font-lock was called (through
  4500      handle_fontified_prop), and jit-lock-functions remove text
  4501      properties or overlays from the portion of buffer that includes
  4502      CHARPOS.  Muse mode is known to do that, for example.  In this
  4503      case, we return -1 to the caller, to signal that no display
  4504      string is actually present at CHARPOS.  See bidi_fetch_char for
  4505      how this is handled.
  4506 
  4507      An alternative would be to never look for display properties past
  4508      it->stop_charpos.  But neither compute_display_string_pos nor
  4509      bidi_fetch_char that calls it know or care where the next
  4510      stop_charpos is.  */
  4511   if (NILP (Fget_char_property (pos, Qdisplay, object)))
  4512     return -1;
  4513 
  4514   /* Look forward for the first character where the `display' property
  4515      changes.  */
  4516   pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
  4517 
  4518   return XFIXNAT (pos);
  4519 }
  4520 
  4521 
  4522 
  4523 /***********************************************************************
  4524                             Fontification
  4525  ***********************************************************************/
  4526 
  4527 /* Handle changes in the `fontified' property of the current buffer by
  4528    calling hook functions from Qfontification_functions to fontify
  4529    regions of text.  */
  4530 
  4531 static enum prop_handled
  4532 handle_fontified_prop (struct it *it)
  4533 {
  4534   Lisp_Object prop, pos;
  4535   enum prop_handled handled = HANDLED_NORMALLY;
  4536 
  4537   if (!NILP (Vmemory_full))
  4538     return handled;
  4539 
  4540   /* Get the value of the `fontified' property at IT's current buffer
  4541      position.  (The `fontified' property doesn't have a special
  4542      meaning in strings.)  If the value is nil, call functions from
  4543      Qfontification_functions.  */
  4544   if (!STRINGP (it->string)
  4545       && it->s == NULL
  4546       && !NILP (Vfontification_functions)
  4547       && !(input_was_pending && redisplay_skip_fontification_on_input)
  4548       && !NILP (Vrun_hooks)
  4549       && (pos = make_fixnum (IT_CHARPOS (*it)),
  4550           prop = Fget_char_property (pos, Qfontified, Qnil),
  4551           /* Ignore the special cased nil value always present at EOB since
  4552              no amount of fontifying will be able to change it.  */
  4553           NILP (prop) && IT_CHARPOS (*it) < Z))
  4554     {
  4555       specpdl_ref count = SPECPDL_INDEX ();
  4556       Lisp_Object val;
  4557       struct buffer *obuf = current_buffer;
  4558       ptrdiff_t begv = BEGV, zv = ZV;
  4559       bool old_clip_changed = current_buffer->clip_changed;
  4560       bool saved_inhibit_flag = it->f->inhibit_clear_image_cache;
  4561 
  4562       val = Vfontification_functions;
  4563       specbind (Qfontification_functions, Qnil);
  4564 
  4565       eassert (it->end_charpos == ZV);
  4566 
  4567       if (current_buffer->long_line_optimizations_p
  4568           && long_line_optimizations_region_size > 0)
  4569         {
  4570           ptrdiff_t begv = it->large_narrowing_begv;
  4571           ptrdiff_t zv = it->large_narrowing_zv;
  4572           ptrdiff_t charpos = IT_CHARPOS (*it);
  4573           if (charpos < begv || charpos > zv)
  4574             {
  4575               begv = get_large_narrowing_begv (charpos);
  4576               zv = get_large_narrowing_zv (charpos);
  4577             }
  4578           if (begv != BEG || zv != Z)
  4579             labeled_narrow_to_region (make_fixnum (begv), make_fixnum (zv),
  4580                                       Qlong_line_optimizations_in_fontification_functions);
  4581         }
  4582 
  4583       /* Don't allow Lisp that runs from 'fontification-functions'
  4584          clear our face and image caches behind our back.  */
  4585       it->f->inhibit_clear_image_cache = true;
  4586 
  4587       if (!CONSP (val) || EQ (XCAR (val), Qlambda))
  4588         safe_call1 (val, pos);
  4589       else
  4590         {
  4591           Lisp_Object fns, fn;
  4592 
  4593           fns = Qnil;
  4594 
  4595           for (; CONSP (val); val = XCDR (val))
  4596             {
  4597               fn = XCAR (val);
  4598 
  4599               if (EQ (fn, Qt))
  4600                 {
  4601                   /* A value of t indicates this hook has a local
  4602                      binding; it means to run the global binding too.
  4603                      In a global value, t should not occur.  If it
  4604                      does, we must ignore it to avoid an endless
  4605                      loop.  */
  4606                   for (fns = Fdefault_value (Qfontification_functions);
  4607                        CONSP (fns);
  4608                        fns = XCDR (fns))
  4609                     {
  4610                       fn = XCAR (fns);
  4611                       if (!EQ (fn, Qt))
  4612                         safe_call1 (fn, pos);
  4613                     }
  4614                 }
  4615               else
  4616                 safe_call1 (fn, pos);
  4617             }
  4618         }
  4619 
  4620       it->f->inhibit_clear_image_cache = saved_inhibit_flag;
  4621       unbind_to (count, Qnil);
  4622 
  4623       /* Fontification functions routinely call `save-restriction'.
  4624          Normally, this tags clip_changed, which can confuse redisplay
  4625          (see discussion in Bug#6671).  Since we don't perform any
  4626          special handling of fontification changes in the case where
  4627          `save-restriction' isn't called, there's no point doing so in
  4628          this case either.  So, if the buffer's restrictions are
  4629          actually left unchanged, reset clip_changed.  */
  4630       if (obuf == current_buffer)
  4631         {
  4632           if (begv == BEGV && zv == ZV)
  4633             current_buffer->clip_changed = old_clip_changed;
  4634         }
  4635       /* There isn't much we can reasonably do to protect against
  4636          misbehaving fontification, but here's a fig leaf.  */
  4637       else if (BUFFER_LIVE_P (obuf))
  4638         set_buffer_internal_1 (obuf);
  4639 
  4640       /* The fontification code may have added/removed text.
  4641          It could do even a lot worse, but let's at least protect against
  4642          the most obvious case where only the text past `pos' gets changed',
  4643          as is/was done in grep.el where some escapes sequences are turned
  4644          into face properties (bug#7876).  */
  4645       it->end_charpos = ZV;
  4646 
  4647       /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
  4648          something.  This avoids an endless loop if they failed to
  4649          fontify the text for which reason ever.  */
  4650       if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
  4651         handled = HANDLED_RECOMPUTE_PROPS;
  4652     }
  4653 
  4654   return handled;
  4655 }
  4656 
  4657 
  4658 
  4659 /***********************************************************************
  4660                                 Faces
  4661  ***********************************************************************/
  4662 
  4663 static int
  4664 face_at_pos (const struct it *it, enum lface_attribute_index attr_filter)
  4665 {
  4666   ptrdiff_t next_stop;
  4667 
  4668   if (!STRINGP (it->string))
  4669     {
  4670       return face_at_buffer_position (it->w,
  4671                                       IT_CHARPOS (*it),
  4672                                       &next_stop,
  4673                                       (IT_CHARPOS (*it)
  4674                                        + TEXT_PROP_DISTANCE_LIMIT),
  4675                                       false, it->base_face_id,
  4676                                       attr_filter);
  4677     }
  4678   else
  4679     {
  4680       int base_face_id;
  4681       ptrdiff_t bufpos;
  4682       int i;
  4683       Lisp_Object from_overlay
  4684         = (it->current.overlay_string_index >= 0
  4685            ? it->string_overlays[it->current.overlay_string_index
  4686                                  % OVERLAY_STRING_CHUNK_SIZE]
  4687            : Qnil);
  4688 
  4689       /* See if we got to this string directly or indirectly from
  4690          an overlay property.  That includes the before-string or
  4691          after-string of an overlay, strings in display properties
  4692          provided by an overlay, their text properties, etc.
  4693 
  4694          FROM_OVERLAY is the overlay that brought us here, or nil if none.  */
  4695       if (! NILP (from_overlay))
  4696         for (i = it->sp - 1; i >= 0; i--)
  4697           {
  4698             if (it->stack[i].current.overlay_string_index >= 0)
  4699               from_overlay
  4700                 = it->string_overlays[it->stack[i].current.overlay_string_index
  4701                                       % OVERLAY_STRING_CHUNK_SIZE];
  4702             else if (! NILP (it->stack[i].from_overlay))
  4703               from_overlay = it->stack[i].from_overlay;
  4704 
  4705             if (!NILP (from_overlay))
  4706               break;
  4707           }
  4708 
  4709       if (! NILP (from_overlay))
  4710         {
  4711           bufpos = IT_CHARPOS (*it);
  4712           /* For a string from an overlay, the base face depends
  4713              only on text properties and ignores overlays.  */
  4714           base_face_id
  4715             = face_for_overlay_string (it->w,
  4716                                        IT_CHARPOS (*it),
  4717                                        &next_stop,
  4718                                        (IT_CHARPOS (*it)
  4719                                         + TEXT_PROP_DISTANCE_LIMIT),
  4720                                        false,
  4721                                        from_overlay, attr_filter);
  4722         }
  4723       else
  4724         {
  4725           bufpos = 0;
  4726 
  4727           /* For strings from a `display' property, use the face at
  4728              IT's current buffer position as the base face to merge
  4729              with, so that overlay strings appear in the same face as
  4730              surrounding text, unless they specify their own faces.
  4731              For strings from wrap-prefix and line-prefix properties,
  4732              use the default face, possibly remapped via
  4733              Vface_remapping_alist.  */
  4734           /* Note that the fact that we use the face at _buffer_
  4735              position means that a 'display' property on an overlay
  4736              string will not inherit the face of that overlay string,
  4737              but will instead revert to the face of buffer text
  4738              covered by the overlay.  This is visible, e.g., when the
  4739              overlay specifies a box face, but neither the buffer nor
  4740              the display string do.  This sounds like a design bug,
  4741              but Emacs always did that since v21.1, so changing that
  4742              might be a big deal.  */
  4743           base_face_id = it->string_from_prefix_prop_p
  4744             ? (!NILP (Vface_remapping_alist)
  4745                ? lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
  4746                : DEFAULT_FACE_ID)
  4747             : underlying_face_id (it);
  4748         }
  4749 
  4750       return face_at_string_position (it->w,
  4751                                       it->string,
  4752                                       IT_STRING_CHARPOS (*it),
  4753                                       bufpos,
  4754                                       &next_stop,
  4755                                       base_face_id, false,
  4756                                       attr_filter);
  4757     } /* !STRINGP (it->string) */
  4758 }
  4759 
  4760 
  4761 /* Set up iterator IT from face properties at its current position.
  4762    Called from handle_stop.  */
  4763 static enum prop_handled
  4764 handle_face_prop (struct it *it)
  4765 {
  4766   specpdl_ref count = SPECPDL_INDEX ();
  4767   /* Don't allow the user to quit out of face-merging code, in case
  4768      this is called when redisplaying a non-selected window, with
  4769      point temporarily moved to window-point.  */
  4770   specbind (Qinhibit_quit, Qt);
  4771   const int new_face_id = face_at_pos (it, 0);
  4772   unbind_to (count, Qnil);
  4773 
  4774 
  4775   /* Is this a start of a run of characters with box face?
  4776      Caveat: this can be called for a freshly initialized
  4777      iterator; face_id is -1 in this case.  We know that the new
  4778      face will not change until limit, i.e. if the new face has a
  4779      box, all characters up to limit will have one.  But, as
  4780      usual, we don't know whether limit is really the end.  */
  4781   if (new_face_id != it->face_id)
  4782     {
  4783       struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
  4784       /* If it->face_id is -1, old_face below will be NULL, see
  4785          the definition of FACE_FROM_ID_OR_NULL.  This will happen
  4786          if this is the initial call that gets the face.  */
  4787       struct face *old_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  4788 
  4789       /* If the value of face_id of the iterator is -1, we have to
  4790          look in front of IT's position and see whether there is a
  4791          face there that's different from new_face_id.  */
  4792       if (!STRINGP (it->string)
  4793           && !old_face
  4794           && IT_CHARPOS (*it) > BEG)
  4795         {
  4796           const int prev_face_id = face_before_it_pos (it);
  4797 
  4798           old_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  4799         }
  4800 
  4801       /* If the new face has a box, but the old face does not,
  4802          this is the start of a run of characters with box face,
  4803          i.e. this character has a shadow on the left side.  */
  4804       it->face_id = new_face_id;
  4805       /* Don't reset the start_of_box_run_p flag, only set it if
  4806          needed.  */
  4807       if (!(it->start_of_box_run_p && old_face && old_face->box))
  4808         it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
  4809                                   && (old_face == NULL || !old_face->box));
  4810       it->face_box_p = new_face->box != FACE_NO_BOX;
  4811     }
  4812 
  4813   return HANDLED_NORMALLY;
  4814 }
  4815 
  4816 
  4817 /* Return the ID of the face ``underlying'' IT's current position,
  4818    which is in a string.  If the iterator is associated with a
  4819    buffer, return the face at IT's current buffer position.
  4820    Otherwise, use the iterator's base_face_id.  */
  4821 
  4822 static int
  4823 underlying_face_id (const struct it *it)
  4824 {
  4825   int face_id = it->base_face_id, i;
  4826 
  4827   eassert (STRINGP (it->string));
  4828 
  4829   for (i = it->sp - 1; i >= 0; --i)
  4830     if (NILP (it->stack[i].string))
  4831       face_id = it->stack[i].face_id;
  4832 
  4833   return face_id;
  4834 }
  4835 
  4836 
  4837 /* Compute the face one character before or after the current position
  4838    of IT, in the visual order.  BEFORE_P means get the face
  4839    in front (to the left in L2R paragraphs, to the right in R2L
  4840    paragraphs) of IT's screen position.  Value is the ID of the face.  */
  4841 
  4842 static int
  4843 face_before_or_after_it_pos (struct it *it, bool before_p)
  4844 {
  4845   int face_id, limit;
  4846   ptrdiff_t next_check_charpos;
  4847   struct it it_copy;
  4848   void *it_copy_data = NULL;
  4849 
  4850   eassert (it->s == NULL);
  4851 
  4852   if (STRINGP (it->string))
  4853     {
  4854       ptrdiff_t bufpos, charpos;
  4855       int base_face_id;
  4856 
  4857       /* No face change past the end of the string (for the case we
  4858          are padding with spaces).  No face change before the string
  4859          start.  Ignore face changes before the first visible
  4860          character on this display line.  */
  4861       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
  4862           || (IT_STRING_CHARPOS (*it) == 0 && before_p)
  4863           || it->current_x <= it->first_visible_x)
  4864         return it->face_id;
  4865 
  4866       if (!it->bidi_p)
  4867         {
  4868           /* Set charpos to the position before or after IT's current
  4869              position, in the logical order, which in the non-bidi
  4870              case is the same as the visual order.  */
  4871           if (before_p)
  4872             charpos = IT_STRING_CHARPOS (*it) - 1;
  4873           else if (it->what == IT_COMPOSITION)
  4874             /* For composition, we must check the character after the
  4875                composition.  */
  4876             charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
  4877           else
  4878             charpos = IT_STRING_CHARPOS (*it) + 1;
  4879         }
  4880       else
  4881         {
  4882           /* With bidi iteration, the character before the current in
  4883              the visual order cannot be found by simple iteration,
  4884              because "reverse" reordering is not supported.  Instead,
  4885              we need to start from the string beginning and go all the
  4886              way to the current string position, remembering the
  4887              visually-previous position.  We need to start from the
  4888              string beginning for the character after the current as
  4889              well, since the iterator state in IT may have been
  4890              pushed, and the bidi cache is no longer coherent with the
  4891              string's text.  */
  4892           SAVE_IT (it_copy, *it, it_copy_data);
  4893           IT_STRING_CHARPOS (it_copy) = 0;
  4894           bidi_init_it (0, 0, FRAME_WINDOW_P (it_copy.f), &it_copy.bidi_it);
  4895           it_copy.bidi_it.scan_dir = 0;
  4896 
  4897           do
  4898             {
  4899               charpos = it_copy.bidi_it.charpos;
  4900               if (charpos >= SCHARS (it->string))
  4901                 break;
  4902               bidi_move_to_visually_next (&it_copy.bidi_it);
  4903             }
  4904           while (it_copy.bidi_it.charpos != IT_STRING_CHARPOS (*it));
  4905 
  4906           if (!before_p)
  4907             {
  4908               /* Set charpos to the string position of the character
  4909                  that comes after IT's current position in the visual
  4910                  order.  */
  4911               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  4912               /* If this is the first string character,
  4913                  bidi_move_to_visually_next will deliver character at
  4914                  current position without moving, so we need to enlarge N.  */
  4915               if (it_copy.bidi_it.first_elt)
  4916                 n++;
  4917               while (n--)
  4918                 bidi_move_to_visually_next (&it_copy.bidi_it);
  4919 
  4920               charpos = it_copy.bidi_it.charpos;
  4921             }
  4922 
  4923           RESTORE_IT (it, it, it_copy_data);
  4924         }
  4925       eassert (0 <= charpos && charpos <= SCHARS (it->string));
  4926 
  4927       if (it->current.overlay_string_index >= 0)
  4928         bufpos = IT_CHARPOS (*it);
  4929       else
  4930         bufpos = 0;
  4931 
  4932       base_face_id = underlying_face_id (it);
  4933 
  4934       /* Get the face for ASCII, or unibyte.  */
  4935       face_id = face_at_string_position (it->w, it->string, charpos,
  4936                                          bufpos, &next_check_charpos,
  4937                                          base_face_id, false, 0);
  4938 
  4939       /* Correct the face for charsets different from ASCII.  Do it
  4940          for the multibyte case only.  The face returned above is
  4941          suitable for unibyte text if IT->string is unibyte.  */
  4942       if (STRING_MULTIBYTE (it->string))
  4943         {
  4944           struct text_pos pos1 = string_pos (charpos, it->string);
  4945           const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
  4946           struct face *face = FACE_FROM_ID (it->f, face_id);
  4947           int len, c = check_char_and_length (p, &len);
  4948           face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
  4949         }
  4950     }
  4951   else
  4952     {
  4953       struct text_pos pos;
  4954 
  4955       if ((IT_CHARPOS (*it) >= ZV && !before_p)
  4956           || (IT_CHARPOS (*it) <= BEGV && before_p))
  4957         return it->face_id;
  4958 
  4959       limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
  4960       pos = it->current.pos;
  4961 
  4962       if (!it->bidi_p)
  4963         {
  4964           if (before_p)
  4965             DEC_TEXT_POS (pos, it->multibyte_p);
  4966           else
  4967             {
  4968               if (it->what == IT_COMPOSITION)
  4969                 {
  4970                   /* For composition, we must check the position after
  4971                      the composition.  */
  4972                   pos.charpos += it->cmp_it.nchars;
  4973                   pos.bytepos += it->len;
  4974                 }
  4975               else
  4976                 INC_TEXT_POS (pos, it->multibyte_p);
  4977             }
  4978         }
  4979       else
  4980         {
  4981           if (before_p)
  4982             {
  4983               int current_x;
  4984 
  4985               /* With bidi iteration, the character before the current
  4986                  in the visual order cannot be found by simple
  4987                  iteration, because "reverse" reordering is not
  4988                  supported.  Instead, we need to use the move_it_*
  4989                  family of functions, and move to the previous
  4990                  character starting from the beginning of the visual
  4991                  line.  */
  4992               /* Ignore face changes before the first visible
  4993                  character on this display line.  */
  4994               if (it->current_x <= it->first_visible_x)
  4995                 return it->face_id;
  4996               SAVE_IT (it_copy, *it, it_copy_data);
  4997               /* Implementation note: Since move_it_in_display_line
  4998                  works in the iterator geometry, and thinks the first
  4999                  character is always the leftmost, even in R2L lines,
  5000                  we don't need to distinguish between the R2L and L2R
  5001                  cases here.  */
  5002               current_x = it_copy.current_x;
  5003               move_it_vertically_backward (&it_copy, 0);
  5004               move_it_in_display_line (&it_copy, ZV, current_x - 1, MOVE_TO_X);
  5005               pos = it_copy.current.pos;
  5006               RESTORE_IT (it, it, it_copy_data);
  5007             }
  5008           else
  5009             {
  5010               /* Set charpos to the buffer position of the character
  5011                  that comes after IT's current position in the visual
  5012                  order.  */
  5013               int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
  5014 
  5015               it_copy = *it;
  5016               /* If this is the first display element,
  5017                  bidi_move_to_visually_next will deliver character at
  5018                  current position without moving, so we need to enlarge N.  */
  5019               if (it->bidi_it.first_elt)
  5020                 n++;
  5021               while (n--)
  5022                 bidi_move_to_visually_next (&it_copy.bidi_it);
  5023 
  5024               SET_TEXT_POS (pos,
  5025                             it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
  5026             }
  5027         }
  5028       eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
  5029 
  5030       /* Determine face for CHARSET_ASCII, or unibyte.  */
  5031       face_id = face_at_buffer_position (it->w,
  5032                                          CHARPOS (pos),
  5033                                          &next_check_charpos,
  5034                                          limit, false, -1, 0);
  5035 
  5036       /* Correct the face for charsets different from ASCII.  Do it
  5037          for the multibyte case only.  The face returned above is
  5038          suitable for unibyte text if current_buffer is unibyte.  */
  5039       if (it->multibyte_p)
  5040         {
  5041           int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
  5042           struct face *face = FACE_FROM_ID (it->f, face_id);
  5043           face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
  5044         }
  5045     }
  5046 
  5047   return face_id;
  5048 }
  5049 
  5050 
  5051 
  5052 /***********************************************************************
  5053                             Invisible text
  5054  ***********************************************************************/
  5055 
  5056 /* Set up iterator IT from invisible properties at its current
  5057    position.  Called from handle_stop.  */
  5058 
  5059 static enum prop_handled
  5060 handle_invisible_prop (struct it *it)
  5061 {
  5062   enum prop_handled handled = HANDLED_NORMALLY;
  5063   int invis;
  5064   Lisp_Object prop;
  5065 
  5066   if (STRINGP (it->string))
  5067     {
  5068       Lisp_Object end_charpos, limit;
  5069 
  5070       /* Get the value of the invisible text property at the
  5071          current position.  Value will be nil if there is no such
  5072          property.  */
  5073       end_charpos = make_fixnum (IT_STRING_CHARPOS (*it));
  5074       prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5075       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5076 
  5077       if (invis != 0 && IT_STRING_CHARPOS (*it) < it->end_charpos)
  5078         {
  5079           /* Record whether we have to display an ellipsis for the
  5080              invisible text.  */
  5081           bool display_ellipsis_p = (invis == 2);
  5082           ptrdiff_t len, endpos;
  5083 
  5084           handled = HANDLED_RECOMPUTE_PROPS;
  5085 
  5086           /* Get the position at which the next visible text can be
  5087              found in IT->string, if any.  */
  5088           endpos = len = SCHARS (it->string);
  5089           XSETINT (limit, len);
  5090           do
  5091             {
  5092               end_charpos
  5093                 = Fnext_single_property_change (end_charpos, Qinvisible,
  5094                                                 it->string, limit);
  5095               /* Since LIMIT is always an integer, so should be the
  5096                  value returned by Fnext_single_property_change.  */
  5097               eassert (FIXNUMP (end_charpos));
  5098               if (FIXNUMP (end_charpos))
  5099                 {
  5100                   endpos = XFIXNAT (end_charpos);
  5101                   prop = Fget_text_property (end_charpos, Qinvisible, it->string);
  5102                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5103                   if (invis == 2)
  5104                     display_ellipsis_p = true;
  5105                 }
  5106               else /* Should never happen; but if it does, exit the loop.  */
  5107                 endpos = len;
  5108             }
  5109           while (invis != 0 && endpos < len);
  5110 
  5111           if (display_ellipsis_p)
  5112             it->ellipsis_p = true;
  5113 
  5114           if (endpos < len)
  5115             {
  5116               /* Text at END_CHARPOS is visible.  Move IT there.  */
  5117               struct text_pos old;
  5118               ptrdiff_t oldpos;
  5119 
  5120               old = it->current.string_pos;
  5121               oldpos = CHARPOS (old);
  5122               if (it->bidi_p)
  5123                 {
  5124                   if (it->bidi_it.first_elt
  5125                       && it->bidi_it.charpos < SCHARS (it->string))
  5126                     bidi_paragraph_init (it->paragraph_embedding,
  5127                                          &it->bidi_it, true);
  5128                   /* Bidi-iterate out of the invisible text.  */
  5129                   do
  5130                     {
  5131                       bidi_move_to_visually_next (&it->bidi_it);
  5132                     }
  5133                   while (oldpos <= it->bidi_it.charpos
  5134                          && it->bidi_it.charpos < endpos
  5135                          && it->bidi_it.charpos < it->bidi_it.string.schars);
  5136 
  5137                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  5138                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  5139                   if (IT_CHARPOS (*it) >= endpos)
  5140                     it->prev_stop = endpos;
  5141                 }
  5142               else
  5143                 {
  5144                   IT_STRING_CHARPOS (*it) = endpos;
  5145                   compute_string_pos (&it->current.string_pos, old, it->string);
  5146                 }
  5147             }
  5148           else
  5149             {
  5150               /* The rest of the string is invisible.  If this is an
  5151                  overlay string, proceed with the next overlay string
  5152                  or whatever comes and return a character from there.  */
  5153               if (it->current.overlay_string_index >= 0
  5154                   && !display_ellipsis_p)
  5155                 {
  5156                   next_overlay_string (it);
  5157                   /* Don't check for overlay strings when we just
  5158                      finished processing them.  */
  5159                   handled = HANDLED_OVERLAY_STRING_CONSUMED;
  5160                 }
  5161               else
  5162                 {
  5163                   IT_STRING_CHARPOS (*it) = SCHARS (it->string);
  5164                   IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
  5165                 }
  5166             }
  5167         }
  5168     }
  5169   else
  5170     {
  5171       ptrdiff_t newpos, next_stop, start_charpos, tem;
  5172       Lisp_Object pos, overlay;
  5173 
  5174       /* First of all, is there invisible text at this position?  */
  5175       tem = start_charpos = IT_CHARPOS (*it);
  5176       pos = make_fixnum (tem);
  5177       prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
  5178                                             &overlay);
  5179       invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5180 
  5181       /* If we are on invisible text, skip over it.  */
  5182       if (invis != 0 && start_charpos < it->end_charpos)
  5183         {
  5184           /* Record whether we have to display an ellipsis for the
  5185              invisible text.  */
  5186           bool display_ellipsis_p = invis == 2;
  5187 
  5188           handled = HANDLED_RECOMPUTE_PROPS;
  5189 
  5190           /* Loop skipping over invisible text.  The loop is left at
  5191              ZV or with IT on the first char being visible again.  */
  5192           do
  5193             {
  5194               /* Try to skip some invisible text.  Return value is the
  5195                  position reached which can be equal to where we start
  5196                  if there is nothing invisible there.  This skips both
  5197                  over invisible text properties and overlays with
  5198                  invisible property.  */
  5199               newpos = skip_invisible (tem, &next_stop, ZV, it->window);
  5200 
  5201               /* If we skipped nothing at all we weren't at invisible
  5202                  text in the first place.  If everything to the end of
  5203                  the buffer was skipped, end the loop.  */
  5204               if (newpos == tem || newpos >= ZV)
  5205                 invis = 0;
  5206               else
  5207                 {
  5208                   /* We skipped some characters but not necessarily
  5209                      all there are.  Check if we ended up on visible
  5210                      text.  Fget_char_property returns the property of
  5211                      the char before the given position, i.e. if we
  5212                      get invis = 0, this means that the char at
  5213                      newpos is visible.  */
  5214                   pos = make_fixnum (newpos);
  5215                   prop = Fget_char_property (pos, Qinvisible, it->window);
  5216                   invis = TEXT_PROP_MEANS_INVISIBLE (prop);
  5217                 }
  5218 
  5219               /* If we ended up on invisible text, proceed to
  5220                  skip starting with next_stop.  */
  5221               if (invis != 0)
  5222                 tem = next_stop;
  5223 
  5224               /* If there are adjacent invisible texts, don't lose the
  5225                  second one's ellipsis.  */
  5226               if (invis == 2)
  5227                 display_ellipsis_p = true;
  5228             }
  5229           while (invis != 0);
  5230 
  5231           /* The position newpos is now either ZV or on visible text.  */
  5232           if (it->bidi_p)
  5233             {
  5234               ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
  5235               bool on_newline
  5236                 = bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
  5237               bool after_newline
  5238                 = newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
  5239 
  5240               /* If the invisible text ends on a newline or on a
  5241                  character after a newline, we can avoid the costly,
  5242                  character by character, bidi iteration to NEWPOS, and
  5243                  instead simply reseat the iterator there.  That's
  5244                  because all bidi reordering information is tossed at
  5245                  the newline.  This is a big win for modes that hide
  5246                  complete lines, like Outline, Org, etc.  */
  5247               if (on_newline || after_newline)
  5248                 {
  5249                   struct text_pos tpos;
  5250                   bidi_dir_t pdir = it->bidi_it.paragraph_dir;
  5251 
  5252                   SET_TEXT_POS (tpos, newpos, bpos);
  5253                   reseat_1 (it, tpos, false);
  5254                   /* If we reseat on a newline/ZV, we need to prep the
  5255                      bidi iterator for advancing to the next character
  5256                      after the newline/EOB, keeping the current paragraph
  5257                      direction (so that PRODUCE_GLYPHS does TRT wrt
  5258                      prepending/appending glyphs to a glyph row).  */
  5259                   if (on_newline)
  5260                     {
  5261                       it->bidi_it.first_elt = false;
  5262                       it->bidi_it.paragraph_dir = pdir;
  5263                       it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
  5264                       it->bidi_it.nchars = 1;
  5265                       it->bidi_it.ch_len = 1;
  5266                     }
  5267                 }
  5268               else      /* Must use the slow method.  */
  5269                 {
  5270                   /* With bidi iteration, the region of invisible text
  5271                      could start and/or end in the middle of a
  5272                      non-base embedding level.  Therefore, we need to
  5273                      skip invisible text using the bidi iterator,
  5274                      starting at IT's current position, until we find
  5275                      ourselves outside of the invisible text.
  5276                      Skipping invisible text _after_ bidi iteration
  5277                      avoids affecting the visual order of the
  5278                      displayed text when invisible properties are
  5279                      added or removed.  */
  5280                   if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
  5281                     {
  5282                       /* If we were `reseat'ed to a new paragraph,
  5283                          determine the paragraph base direction.  We
  5284                          need to do it now because
  5285                          next_element_from_buffer may not have a
  5286                          chance to do it, if we are going to skip any
  5287                          text at the beginning, which resets the
  5288                          FIRST_ELT flag.  */
  5289                       bidi_paragraph_init (it->paragraph_embedding,
  5290                                            &it->bidi_it, true);
  5291                     }
  5292                   do
  5293                     {
  5294                       bidi_move_to_visually_next (&it->bidi_it);
  5295                     }
  5296                   while (it->stop_charpos <= it->bidi_it.charpos
  5297                          && it->bidi_it.charpos < newpos);
  5298                   IT_CHARPOS (*it) = it->bidi_it.charpos;
  5299                   IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  5300                   /* If we overstepped NEWPOS, record its position in
  5301                      the iterator, so that we skip invisible text if
  5302                      later the bidi iteration lands us in the
  5303                      invisible region again. */
  5304                   if (IT_CHARPOS (*it) >= newpos)
  5305                     it->prev_stop = newpos;
  5306                 }
  5307             }
  5308           else
  5309             {
  5310               IT_CHARPOS (*it) = newpos;
  5311               IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
  5312             }
  5313 
  5314           if (display_ellipsis_p)
  5315             {
  5316               /* Make sure that the glyphs of the ellipsis will get
  5317                  correct `charpos' values.  If we would not update
  5318                  it->position here, the glyphs would belong to the
  5319                  last visible character _before_ the invisible
  5320                  text, which confuses `set_cursor_from_row'.
  5321 
  5322                  We use the last invisible position instead of the
  5323                  first because this way the cursor is always drawn on
  5324                  the first "." of the ellipsis, whenever PT is inside
  5325                  the invisible text.  Otherwise the cursor would be
  5326                  placed _after_ the ellipsis when the point is after the
  5327                  first invisible character.  */
  5328               if (!STRINGP (it->object))
  5329                 {
  5330                   it->position.charpos = newpos - 1;
  5331                   it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
  5332                 }
  5333             }
  5334 
  5335           /* If there are before-strings at the start of invisible
  5336              text, and the text is invisible because of a text
  5337              property, arrange to show before-strings because 20.x did
  5338              it that way.  (If the text is invisible because of an
  5339              overlay property instead of a text property, this is
  5340              already handled in the overlay code.)  */
  5341           if (NILP (overlay)
  5342               && get_overlay_strings (it, it->stop_charpos))
  5343             {
  5344               handled = HANDLED_RECOMPUTE_PROPS;
  5345               if (it->sp > 0)
  5346                 {
  5347                   it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
  5348                   /* The call to get_overlay_strings above recomputes
  5349                      it->stop_charpos, but it only considers changes
  5350                      in properties and overlays beyond iterator's
  5351                      current position.  This causes us to miss changes
  5352                      that happen exactly where the invisible property
  5353                      ended.  So we play it safe here and force the
  5354                      iterator to check for potential stop positions
  5355                      immediately after the invisible text.  Note that
  5356                      if get_overlay_strings returns true, it
  5357                      normally also pushed the iterator stack, so we
  5358                      need to update the stop position in the slot
  5359                      below the current one.  */
  5360                   it->stack[it->sp - 1].stop_charpos
  5361                     = CHARPOS (it->stack[it->sp - 1].current.pos);
  5362                 }
  5363             }
  5364           else if (display_ellipsis_p)
  5365             {
  5366               it->ellipsis_p = true;
  5367               /* Let the ellipsis display before
  5368                  considering any properties of the following char.
  5369                  Fixes jasonr@gnu.org 01 Oct 07 bug.  */
  5370               handled = HANDLED_RETURN;
  5371             }
  5372         }
  5373     }
  5374 
  5375   return handled;
  5376 }
  5377 
  5378 
  5379 /* Make iterator IT return `...' next.
  5380    Replaces LEN characters from buffer.  */
  5381 
  5382 static void
  5383 setup_for_ellipsis (struct it *it, int len)
  5384 {
  5385   /* Use the display table definition for `...'.  Invalid glyphs
  5386      will be handled by the method returning elements from dpvec.  */
  5387   if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
  5388     {
  5389       struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
  5390       it->dpvec = v->contents;
  5391       it->dpend = v->contents + v->header.size;
  5392     }
  5393   else
  5394     {
  5395       /* Default `...'.  */
  5396       it->dpvec = default_invis_vector;
  5397       it->dpend = default_invis_vector + 3;
  5398     }
  5399 
  5400   it->dpvec_char_len = len;
  5401   it->current.dpvec_index = 0;
  5402   it->dpvec_face_id = -1;
  5403 
  5404   /* Use IT->saved_face_id for the ellipsis, so that it has the same
  5405      face as the preceding text.  IT->saved_face_id was set in
  5406      handle_stop to the face of the preceding character, and will be
  5407      different from IT->face_id only if the invisible text skipped in
  5408      handle_invisible_prop has some non-default face on its first
  5409      character.  We thus ignore the face of the invisible text when we
  5410      display the ellipsis.  IT's face is restored in set_iterator_to_next.  */
  5411   if (it->saved_face_id >= 0)
  5412     it->face_id = it->saved_face_id;
  5413 
  5414   /* If the ellipsis represents buffer text, it means we advanced in
  5415      the buffer, so we should no longer ignore overlay strings.  */
  5416   if (it->method == GET_FROM_BUFFER)
  5417     it->ignore_overlay_strings_at_pos_p = false;
  5418 
  5419   it->method = GET_FROM_DISPLAY_VECTOR;
  5420   it->ellipsis_p = true;
  5421 }
  5422 
  5423 
  5424 static Lisp_Object
  5425 find_display_property (Lisp_Object disp, Lisp_Object prop)
  5426 {
  5427   if (NILP (disp))
  5428     return Qnil;
  5429   /* We have a vector of display specs.  */
  5430   if (VECTORP (disp))
  5431     {
  5432       for (ptrdiff_t i = 0; i < ASIZE (disp); i++)
  5433         {
  5434           Lisp_Object elem = AREF (disp, i);
  5435           if (CONSP (elem)
  5436               && CONSP (XCDR (elem))
  5437               && EQ (XCAR (elem), prop))
  5438             return XCAR (XCDR (elem));
  5439         }
  5440       return Qnil;
  5441     }
  5442   /* We have a list of display specs.  */
  5443   else if (CONSP (disp)
  5444            && CONSP (XCAR (disp)))
  5445     {
  5446       while (!NILP (disp))
  5447         {
  5448           Lisp_Object elem = XCAR (disp);
  5449           if (CONSP (elem)
  5450               && CONSP (XCDR (elem))
  5451               && EQ (XCAR (elem), prop))
  5452             return XCAR (XCDR (elem));
  5453 
  5454           /* Check that we have a proper list before going to the next
  5455              element.  */
  5456           if (CONSP (XCDR (disp)))
  5457             disp = XCDR (disp);
  5458           else
  5459             disp = Qnil;
  5460         }
  5461       return Qnil;
  5462     }
  5463   /* A simple display spec.  */
  5464   else if (CONSP (disp)
  5465            && CONSP (XCDR (disp))
  5466            && EQ (XCAR (disp), prop))
  5467     return XCAR (XCDR (disp));
  5468   else
  5469     return Qnil;
  5470 }
  5471 
  5472 static Lisp_Object
  5473 get_display_property (ptrdiff_t bufpos, Lisp_Object prop, Lisp_Object object)
  5474 {
  5475   return find_display_property (Fget_text_property (make_fixnum (bufpos),
  5476                                                     Qdisplay, object),
  5477                                 prop);
  5478 }
  5479 
  5480 static void
  5481 display_min_width (struct it *it, ptrdiff_t bufpos,
  5482                    Lisp_Object object, Lisp_Object width_spec)
  5483 {
  5484   /* We're being called at the end of the `min-width' sequence,
  5485      probably. */
  5486   if (!NILP (it->min_width_property)
  5487       && !EQ (width_spec, it->min_width_property))
  5488     {
  5489       if (!it->glyph_row)
  5490         return;
  5491 
  5492       /* When called from display_string (i.e., the mode line),
  5493          we're being called with a string as the object, and we
  5494          may be called with many sub-strings belonging to the same
  5495          :propertize run. */
  5496       if ((bufpos == 0
  5497            && !EQ (it->min_width_property,
  5498                    get_display_property (0, Qmin_width, object)))
  5499           /* In a buffer -- check that we're really right after the
  5500              sequence of characters covered by this `min-width'.  */
  5501           || (bufpos > BEGV
  5502               && EQ (it->min_width_property,
  5503                      get_display_property (bufpos - 1, Qmin_width, object))))
  5504         {
  5505           Lisp_Object w = Qnil;
  5506           double width;
  5507 #ifdef HAVE_WINDOW_SYSTEM
  5508           if (FRAME_WINDOW_P (it->f))
  5509             {
  5510               struct font *font = NULL;
  5511               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5512               font = face->font ? face->font : FRAME_FONT (it->f);
  5513               calc_pixel_width_or_height (&width, it,
  5514                                           XCAR (it->min_width_property),
  5515                                           font, true, NULL);
  5516               width -= it->current_x - it->min_width_start;
  5517               w = list1 (make_int (width));
  5518             }
  5519           else
  5520 #endif
  5521             {
  5522               calc_pixel_width_or_height (&width, it,
  5523                                           XCAR (it->min_width_property),
  5524                                           NULL, true, NULL);
  5525               width -= (it->current_x - it->min_width_start) /
  5526                 FRAME_COLUMN_WIDTH (it->f);
  5527               w = make_int (width);
  5528             }
  5529 
  5530           /* Insert the stretch glyph.  */
  5531           it->object = list3 (Qspace, QCwidth, w);
  5532           produce_stretch_glyph (it);
  5533           if (it->area == TEXT_AREA)
  5534             it->current_x += it->pixel_width;
  5535           it->min_width_property = Qnil;
  5536         }
  5537     }
  5538 
  5539   /* We're at the start of a `min-width' sequence -- record the
  5540      position and the property, so that we can later see if we're at
  5541      the end.  */
  5542   if (CONSP (width_spec))
  5543     {
  5544       if (bufpos == BEGV
  5545           /* Mode line (see above).  */
  5546           || (bufpos == 0
  5547               && !EQ (it->min_width_property,
  5548                       get_display_property (0, Qmin_width, object)))
  5549           /* Buffer.  */
  5550           || (bufpos > BEGV
  5551               && !EQ (width_spec,
  5552                       get_display_property (bufpos - 1, Qmin_width, object))))
  5553         {
  5554           it->min_width_property = width_spec;
  5555           it->min_width_start = it->current_x;
  5556         }
  5557     }
  5558 }
  5559 
  5560 DEFUN ("get-display-property", Fget_display_property,
  5561        Sget_display_property, 2, 4, 0,
  5562        doc: /* Get the value of the `display' property PROP at POSITION.
  5563 If OBJECT, this should be a buffer or string where the property is
  5564 fetched from.  If omitted, OBJECT defaults to the current buffer.
  5565 
  5566 If PROPERTIES, look for value of PROP in PROPERTIES instead of the
  5567 properties at POSITION.  */)
  5568   (Lisp_Object position, Lisp_Object prop, Lisp_Object object,
  5569    Lisp_Object properties)
  5570 {
  5571   if (NILP (properties))
  5572     properties = Fget_text_property (position, Qdisplay, object);
  5573   else
  5574     CHECK_LIST (properties);
  5575 
  5576   return find_display_property (properties, prop);
  5577 }
  5578 
  5579 
  5580 
  5581 /***********************************************************************
  5582                             'display' property
  5583  ***********************************************************************/
  5584 
  5585 /* Set up iterator IT from `display' property at its current position.
  5586    Called from handle_stop.
  5587    We return HANDLED_RETURN if some part of the display property
  5588    overrides the display of the buffer text itself.
  5589    Otherwise we return HANDLED_NORMALLY.  */
  5590 
  5591 static enum prop_handled
  5592 handle_display_prop (struct it *it)
  5593 {
  5594   Lisp_Object propval, object, overlay;
  5595   struct text_pos *position;
  5596   ptrdiff_t bufpos;
  5597   /* Nonzero if some property replaces the display of the text itself.  */
  5598   int display_replaced = 0;
  5599 
  5600   if (STRINGP (it->string))
  5601     {
  5602       object = it->string;
  5603       position = &it->current.string_pos;
  5604       bufpos = CHARPOS (it->current.pos);
  5605     }
  5606   else
  5607     {
  5608       XSETWINDOW (object, it->w);
  5609       position = &it->current.pos;
  5610       bufpos = CHARPOS (*position);
  5611     }
  5612 
  5613   /* Reset those iterator values set from display property values.  */
  5614   it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
  5615   it->space_width = Qnil;
  5616   it->font_height = Qnil;
  5617   it->voffset = 0;
  5618 
  5619   /* We don't support recursive `display' properties, i.e. string
  5620      values that have a string `display' property, that have a string
  5621      `display' property etc.  */
  5622   if (!it->string_from_display_prop_p)
  5623     it->area = TEXT_AREA;
  5624 
  5625   propval = get_char_property_and_overlay (make_fixnum (position->charpos),
  5626                                            Qdisplay, object, &overlay);
  5627 
  5628   /* Rest of the code must have OBJECT be either a string or a buffer.  */
  5629   if (!STRINGP (it->string))
  5630     object = it->w->contents;
  5631 
  5632   /* Handle min-width ends. */
  5633   if (!NILP (it->min_width_property)
  5634       && NILP (find_display_property (propval, Qmin_width)))
  5635     display_min_width (it, bufpos, object, Qnil);
  5636 
  5637   if (NILP (propval))
  5638     return HANDLED_NORMALLY;
  5639   /* Now OVERLAY is the overlay that gave us this property, or nil
  5640      if it was a text property.  */
  5641 
  5642   display_replaced = handle_display_spec (it, propval, object, overlay,
  5643                                           position, bufpos,
  5644                                           FRAME_WINDOW_P (it->f));
  5645   return display_replaced != 0 ? HANDLED_RETURN : HANDLED_NORMALLY;
  5646 }
  5647 
  5648 /* Subroutine of handle_display_prop.  Returns non-zero if the display
  5649    specification in SPEC is a replacing specification, i.e. it would
  5650    replace the text covered by `display' property with something else,
  5651    such as an image or a display string.  If SPEC includes any kind or
  5652    `(space ...) specification, the value is 2; this is used by
  5653    compute_display_string_pos, which see.
  5654 
  5655    See handle_single_display_spec for documentation of arguments.
  5656    FRAME_WINDOW_P is true if the window being redisplayed is on a
  5657    GUI frame; this argument is used only if IT is NULL, see below.
  5658 
  5659    IT can be NULL, if this is called by the bidi reordering code
  5660    through compute_display_string_pos, which see.  In that case, this
  5661    function only examines SPEC, but does not otherwise "handle" it, in
  5662    the sense that it doesn't set up members of IT from the display
  5663    spec.  */
  5664 static int
  5665 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5666                      Lisp_Object overlay, struct text_pos *position,
  5667                      ptrdiff_t bufpos, bool frame_window_p)
  5668 {
  5669   int replacing = 0;
  5670   bool enable_eval = true;
  5671 
  5672   /* Support (disable-eval PROP) which is used by enriched.el.  */
  5673   if (CONSP (spec) && EQ (XCAR (spec), Qdisable_eval))
  5674     {
  5675       enable_eval = false;
  5676       spec = CONSP (XCDR (spec)) ? XCAR (XCDR (spec)) : Qnil;
  5677     }
  5678 
  5679   if (CONSP (spec)
  5680       /* Simple specifications.  */
  5681       && !EQ (XCAR (spec), Qimage)
  5682 #ifdef HAVE_XWIDGETS
  5683       && !EQ (XCAR (spec), Qxwidget)
  5684 #endif
  5685       && !EQ (XCAR (spec), Qspace)
  5686       && !EQ (XCAR (spec), Qwhen)
  5687       && !EQ (XCAR (spec), Qslice)
  5688       && !EQ (XCAR (spec), Qspace_width)
  5689       && !EQ (XCAR (spec), Qheight)
  5690       && !EQ (XCAR (spec), Qraise)
  5691       /* Marginal area specifications.  */
  5692       && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
  5693       && !EQ (XCAR (spec), Qleft_fringe)
  5694       && !EQ (XCAR (spec), Qright_fringe)
  5695       && !EQ (XCAR (spec), Qmin_width)
  5696       && !NILP (XCAR (spec)))
  5697     {
  5698       for (; CONSP (spec); spec = XCDR (spec))
  5699         {
  5700           int rv = handle_single_display_spec (it, XCAR (spec), object,
  5701                                                overlay, position, bufpos,
  5702                                                replacing, frame_window_p,
  5703                                                enable_eval);
  5704           if (rv != 0)
  5705             {
  5706               replacing = rv;
  5707               /* If some text in a string is replaced, `position' no
  5708                  longer points to the position of `object'.  */
  5709               if (!it || STRINGP (object))
  5710                 break;
  5711             }
  5712         }
  5713     }
  5714   else if (VECTORP (spec))
  5715     {
  5716       ptrdiff_t i;
  5717       for (i = 0; i < ASIZE (spec); ++i)
  5718         {
  5719           int rv = handle_single_display_spec (it, AREF (spec, i), object,
  5720                                                overlay, position, bufpos,
  5721                                                replacing, frame_window_p,
  5722                                                enable_eval);
  5723           if (rv != 0)
  5724             {
  5725               replacing = rv;
  5726               /* If some text in a string is replaced, `position' no
  5727                  longer points to the position of `object'.  */
  5728               if (!it || STRINGP (object))
  5729                 break;
  5730             }
  5731         }
  5732     }
  5733   else
  5734     replacing = handle_single_display_spec (it, spec, object, overlay, position,
  5735                                             bufpos, 0, frame_window_p,
  5736                                             enable_eval);
  5737   return replacing;
  5738 }
  5739 
  5740 /* Value is the position of the end of the `display' property starting
  5741    at START_POS in OBJECT.  */
  5742 
  5743 static struct text_pos
  5744 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
  5745 {
  5746   Lisp_Object end;
  5747   struct text_pos end_pos;
  5748 
  5749   end = Fnext_single_char_property_change (make_fixnum (CHARPOS (start_pos)),
  5750                                            Qdisplay, object, Qnil);
  5751   CHARPOS (end_pos) = XFIXNAT (end);
  5752   if (STRINGP (object))
  5753     compute_string_pos (&end_pos, start_pos, it->string);
  5754   else
  5755     BYTEPOS (end_pos) = CHAR_TO_BYTE (XFIXNAT (end));
  5756 
  5757   return end_pos;
  5758 }
  5759 
  5760 
  5761 /* Set up IT from a single `display' property specification SPEC.  OBJECT
  5762    is the object in which the `display' property was found.  *POSITION
  5763    is the position in OBJECT at which the `display' property was found.
  5764    BUFPOS is the buffer position of OBJECT (different from POSITION if
  5765    OBJECT is not a buffer).  DISPLAY_REPLACED non-zero means that we
  5766    previously saw a display specification which already replaced text
  5767    display with something else, for example an image; we ignore such
  5768    properties after the first one has been processed.
  5769 
  5770    OVERLAY is the overlay this `display' property came from,
  5771    or nil if it was a text property.
  5772 
  5773    If SPEC is a `space' or `image' specification, and in some other
  5774    cases too, set *POSITION to the position where the `display'
  5775    property ends.
  5776 
  5777    If IT is NULL, only examine the property specification in SPEC, but
  5778    don't set up IT.  In that case, FRAME_WINDOW_P means SPEC
  5779    is intended to be displayed in a window on a GUI frame.
  5780 
  5781    Enable evaluation of Lisp forms only if ENABLE_EVAL_P is true.
  5782 
  5783    Value is non-zero if something was found which replaces the display
  5784    of buffer or string text.  */
  5785 
  5786 static int
  5787 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
  5788                             Lisp_Object overlay, struct text_pos *position,
  5789                             ptrdiff_t bufpos, int display_replaced,
  5790                             bool frame_window_p, bool enable_eval_p)
  5791 {
  5792   Lisp_Object form;
  5793   Lisp_Object location, value;
  5794   struct text_pos start_pos = *position;
  5795   void *itdata = NULL;
  5796 
  5797   /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
  5798      If the result is non-nil, use VALUE instead of SPEC.  */
  5799   form = Qt;
  5800   if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
  5801     {
  5802       spec = XCDR (spec);
  5803       if (!CONSP (spec))
  5804         return 0;
  5805       form = XCAR (spec);
  5806       spec = XCDR (spec);
  5807     }
  5808 
  5809   if (!NILP (form) && !EQ (form, Qt) && !enable_eval_p)
  5810     form = Qnil;
  5811   if (!NILP (form) && !EQ (form, Qt))
  5812     {
  5813       specpdl_ref count = SPECPDL_INDEX ();
  5814 
  5815       /* Bind `object' to the object having the `display' property, a
  5816          buffer or string.  Bind `position' to the position in the
  5817          object where the property was found, and `buffer-position'
  5818          to the current position in the buffer.  */
  5819 
  5820       if (NILP (object))
  5821         XSETBUFFER (object, current_buffer);
  5822       specbind (Qobject, object);
  5823       specbind (Qposition, make_fixnum (CHARPOS (*position)));
  5824       specbind (Qbuffer_position, make_fixnum (bufpos));
  5825       /* Save and restore the bidi cache, since FORM could be crazy
  5826          enough to re-enter redisplay, e.g., by calling 'message'.  */
  5827       itdata = bidi_shelve_cache ();
  5828       form = safe_eval (form);
  5829       bidi_unshelve_cache (itdata, false);
  5830       form = unbind_to (count, form);
  5831     }
  5832 
  5833   if (NILP (form))
  5834     return 0;
  5835 
  5836   /* Handle `(height HEIGHT)' specifications.  */
  5837   if (CONSP (spec)
  5838       && EQ (XCAR (spec), Qheight)
  5839       && CONSP (XCDR (spec)))
  5840     {
  5841       if (it)
  5842         {
  5843           if (!FRAME_WINDOW_P (it->f))
  5844             return 0;
  5845 
  5846           it->font_height = XCAR (XCDR (spec));
  5847           if (!NILP (it->font_height))
  5848             {
  5849               int new_height = -1;
  5850 
  5851               if (CONSP (it->font_height)
  5852                   && (EQ (XCAR (it->font_height), Qplus)
  5853                       || EQ (XCAR (it->font_height), Qminus))
  5854                   && CONSP (XCDR (it->font_height))
  5855                   && RANGED_FIXNUMP (0, XCAR (XCDR (it->font_height)), INT_MAX))
  5856                 {
  5857                   /* `(+ N)' or `(- N)' where N is an integer.  */
  5858                   int steps = XFIXNUM (XCAR (XCDR (it->font_height)));
  5859                   if (EQ (XCAR (it->font_height), Qplus))
  5860                     steps = - steps;
  5861                   it->face_id = smaller_face (it->f, it->face_id, steps);
  5862                 }
  5863               else if (FUNCTIONP (it->font_height) && enable_eval_p)
  5864                 {
  5865                   /* Call function with current height as argument.
  5866                      Value is the new height.  */
  5867                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5868                   Lisp_Object height;
  5869                   itdata = bidi_shelve_cache ();
  5870                   height = safe_call1 (it->font_height,
  5871                                        face->lface[LFACE_HEIGHT_INDEX]);
  5872                   bidi_unshelve_cache (itdata, false);
  5873                   if (NUMBERP (height))
  5874                     new_height = XFLOATINT (height);
  5875                 }
  5876               else if (NUMBERP (it->font_height))
  5877                 {
  5878                   /* Value is a multiple of the canonical char height.  */
  5879                   struct face *f;
  5880 
  5881                   f = FACE_FROM_ID (it->f,
  5882                                     lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID));
  5883                   new_height = (XFLOATINT (it->font_height)
  5884                                 * XFIXNUM (f->lface[LFACE_HEIGHT_INDEX]));
  5885                 }
  5886               else if (enable_eval_p)
  5887                 {
  5888                   /* Evaluate IT->font_height with `height' bound to the
  5889                      current specified height to get the new height.  */
  5890                   specpdl_ref count = SPECPDL_INDEX ();
  5891                   struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5892 
  5893                   specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
  5894                   itdata = bidi_shelve_cache ();
  5895                   value = safe_eval (it->font_height);
  5896                   bidi_unshelve_cache (itdata, false);
  5897                   value = unbind_to (count, value);
  5898 
  5899                   if (NUMBERP (value))
  5900                     new_height = XFLOATINT (value);
  5901                 }
  5902 
  5903               if (new_height > 0)
  5904                 it->face_id = face_with_height (it->f, it->face_id, new_height);
  5905             }
  5906         }
  5907 
  5908       return 0;
  5909     }
  5910 
  5911   /* Handle `(space-width WIDTH)'.  */
  5912   if (CONSP (spec)
  5913       && EQ (XCAR (spec), Qspace_width)
  5914       && CONSP (XCDR (spec)))
  5915     {
  5916       if (it)
  5917         {
  5918           if (!FRAME_WINDOW_P (it->f))
  5919             return 0;
  5920 
  5921           value = XCAR (XCDR (spec));
  5922           if (NUMBERP (value) && XFLOATINT (value) > 0)
  5923             it->space_width = value;
  5924         }
  5925 
  5926       return 0;
  5927     }
  5928 
  5929   /* Handle `(min-width (WIDTH))'.  */
  5930   if (CONSP (spec)
  5931       && EQ (XCAR (spec), Qmin_width)
  5932       && CONSP (XCDR (spec))
  5933       && CONSP (XCAR (XCDR (spec))))
  5934     {
  5935       if (it)
  5936         display_min_width (it, bufpos, object, XCAR (XCDR (spec)));
  5937       return 0;
  5938     }
  5939 
  5940   /* Handle `(slice X Y WIDTH HEIGHT)'.  */
  5941   if (CONSP (spec)
  5942       && EQ (XCAR (spec), Qslice))
  5943     {
  5944       Lisp_Object tem;
  5945 
  5946       if (it)
  5947         {
  5948           if (!FRAME_WINDOW_P (it->f))
  5949             return 0;
  5950 
  5951           if (tem = XCDR (spec), CONSP (tem))
  5952             {
  5953               it->slice.x = XCAR (tem);
  5954               if (tem = XCDR (tem), CONSP (tem))
  5955                 {
  5956                   it->slice.y = XCAR (tem);
  5957                   if (tem = XCDR (tem), CONSP (tem))
  5958                     {
  5959                       it->slice.width = XCAR (tem);
  5960                       if (tem = XCDR (tem), CONSP (tem))
  5961                         it->slice.height = XCAR (tem);
  5962                     }
  5963                 }
  5964             }
  5965         }
  5966 
  5967       return 0;
  5968     }
  5969 
  5970   /* Handle `(raise FACTOR)'.  */
  5971   if (CONSP (spec)
  5972       && EQ (XCAR (spec), Qraise)
  5973       && CONSP (XCDR (spec)))
  5974     {
  5975       if (it)
  5976         {
  5977           if (!FRAME_WINDOW_P (it->f))
  5978             return 0;
  5979 
  5980 #ifdef HAVE_WINDOW_SYSTEM
  5981           value = XCAR (XCDR (spec));
  5982           if (NUMBERP (value))
  5983             {
  5984               struct face *face = FACE_FROM_ID (it->f, it->face_id);
  5985               it->voffset = - (XFLOATINT (value)
  5986                                * (normal_char_height (face->font, -1)));
  5987             }
  5988 #endif /* HAVE_WINDOW_SYSTEM */
  5989         }
  5990 
  5991       return 0;
  5992     }
  5993 
  5994   /* Don't handle the other kinds of display specifications
  5995      inside a string that we got from a `display' property.  */
  5996   if (it && it->string_from_display_prop_p)
  5997     return 0;
  5998 
  5999   /* Characters having this form of property are not displayed, so
  6000      we have to find the end of the property.  */
  6001   if (it)
  6002     {
  6003       start_pos = *position;
  6004       *position = display_prop_end (it, object, start_pos);
  6005       /* If the display property comes from an overlay, don't consider
  6006          any potential stop_charpos values before the end of that
  6007          overlay.  Since display_prop_end will happily find another
  6008          'display' property coming from some other overlay or text
  6009          property on buffer positions before this overlay's end, we
  6010          need to ignore them, or else we risk displaying this
  6011          overlay's display string/image twice.  */
  6012       if (!NILP (overlay))
  6013         {
  6014           ptrdiff_t ovendpos = OVERLAY_END (overlay);
  6015 
  6016           /* Some borderline-sane Lisp might call us with the current
  6017              buffer narrowed so that overlay-end is outside the
  6018              POINT_MIN..POINT_MAX region, which will then cause
  6019              various assertion violations and crashes down the road,
  6020              starting with pop_it when it will attempt to use POSITION
  6021              set below.  Prevent that.  */
  6022           ovendpos = clip_to_bounds (BEGV, ovendpos, ZV);
  6023 
  6024           if (ovendpos > CHARPOS (*position))
  6025             SET_TEXT_POS (*position, ovendpos, CHAR_TO_BYTE (ovendpos));
  6026         }
  6027     }
  6028   value = Qnil;
  6029 
  6030   /* Stop the scan at that end position--we assume that all
  6031      text properties change there.  */
  6032   if (it)
  6033     it->stop_charpos = position->charpos;
  6034 
  6035   /* Handle `(left-fringe BITMAP [FACE])'
  6036      and `(right-fringe BITMAP [FACE])'.  */
  6037   if (CONSP (spec)
  6038       && (EQ (XCAR (spec), Qleft_fringe)
  6039           || EQ (XCAR (spec), Qright_fringe))
  6040       && CONSP (XCDR (spec)))
  6041     {
  6042       if (it)
  6043         {
  6044           if (!FRAME_WINDOW_P (it->f))
  6045             /* If we return here, POSITION has been advanced
  6046                across the text with this property.  */
  6047             {
  6048               /* Synchronize the bidi iterator with POSITION.  This is
  6049                  needed because we are not going to push the iterator
  6050                  on behalf of this display property, so there will be
  6051                  no pop_it call to do this synchronization for us.  */
  6052               if (it->bidi_p)
  6053                 {
  6054                   it->position = *position;
  6055                   iterate_out_of_display_property (it);
  6056                   *position = it->position;
  6057                 }
  6058               return 1;
  6059             }
  6060         }
  6061       else if (!frame_window_p)
  6062         return 1;
  6063 
  6064 #ifdef HAVE_WINDOW_SYSTEM
  6065       value = XCAR (XCDR (spec));
  6066       int fringe_bitmap = SYMBOLP (value) ? lookup_fringe_bitmap (value) : 0;
  6067       if (! fringe_bitmap)
  6068         /* If we return here, POSITION has been advanced
  6069            across the text with this property.  */
  6070         {
  6071           if (it && it->bidi_p)
  6072             {
  6073               it->position = *position;
  6074               iterate_out_of_display_property (it);
  6075               *position = it->position;
  6076             }
  6077           return 1;
  6078         }
  6079 
  6080       if (it)
  6081         {
  6082           int face_id = lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
  6083 
  6084           if (CONSP (XCDR (XCDR (spec))))
  6085             {
  6086               Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
  6087               int face_id2;
  6088               /* Don't allow quitting from lookup_derived_face, for when
  6089                  we are displaying a non-selected window, and the buffer's
  6090                  point was temporarily moved to the window-point.  */
  6091               specpdl_ref count1 = SPECPDL_INDEX ();
  6092               specbind (Qinhibit_quit, Qt);
  6093               face_id2 = lookup_derived_face (it->w, it->f, face_name,
  6094                                               FRINGE_FACE_ID, false);
  6095               unbind_to (count1, Qnil);
  6096               if (face_id2 >= 0)
  6097                 face_id = face_id2;
  6098             }
  6099 
  6100           /* Save current settings of IT so that we can restore them
  6101              when we are finished with the glyph property value.  */
  6102           push_it (it, position);
  6103 
  6104           it->area = TEXT_AREA;
  6105           it->what = IT_IMAGE;
  6106           it->image_id = -1; /* no image */
  6107           it->position = start_pos;
  6108           it->object = NILP (object) ? it->w->contents : object;
  6109           it->method = GET_FROM_IMAGE;
  6110           it->from_overlay = Qnil;
  6111           it->face_id = face_id;
  6112           it->from_disp_prop_p = true;
  6113 
  6114           /* Say that we haven't consumed the characters with
  6115              `display' property yet.  The call to pop_it in
  6116              set_iterator_to_next will clean this up.  */
  6117           *position = start_pos;
  6118 
  6119           if (EQ (XCAR (spec), Qleft_fringe))
  6120             {
  6121               it->left_user_fringe_bitmap = fringe_bitmap;
  6122               it->left_user_fringe_face_id = face_id;
  6123             }
  6124           else
  6125             {
  6126               it->right_user_fringe_bitmap = fringe_bitmap;
  6127               it->right_user_fringe_face_id = face_id;
  6128             }
  6129         }
  6130 #endif /* HAVE_WINDOW_SYSTEM */
  6131       return 1;
  6132     }
  6133 
  6134   /* Prepare to handle `((margin left-margin) ...)',
  6135      `((margin right-margin) ...)' and `((margin nil) ...)'
  6136      prefixes for display specifications.  */
  6137   location = Qunbound;
  6138   if (CONSP (spec) && CONSP (XCAR (spec)))
  6139     {
  6140       Lisp_Object tem;
  6141 
  6142       value = XCDR (spec);
  6143       if (CONSP (value))
  6144         value = XCAR (value);
  6145 
  6146       tem = XCAR (spec);
  6147       if (EQ (XCAR (tem), Qmargin)
  6148           && (tem = XCDR (tem),
  6149               tem = CONSP (tem) ? XCAR (tem) : Qnil,
  6150               (NILP (tem)
  6151                || EQ (tem, Qleft_margin)
  6152                || EQ (tem, Qright_margin))))
  6153         location = tem;
  6154     }
  6155 
  6156   if (BASE_EQ (location, Qunbound))
  6157     {
  6158       location = Qnil;
  6159       value = spec;
  6160     }
  6161 
  6162   /* After this point, VALUE is the property after any
  6163      margin prefix has been stripped.  It must be a string,
  6164      an image specification, or `(space ...)'.
  6165 
  6166      LOCATION specifies where to display: `left-margin',
  6167      `right-margin' or nil.  */
  6168 
  6169   bool valid_p = (STRINGP (value)
  6170 #ifdef HAVE_WINDOW_SYSTEM
  6171                   || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6172                       && valid_image_p (value))
  6173 #endif /* not HAVE_WINDOW_SYSTEM */
  6174              || (CONSP (value) && EQ (XCAR (value), Qspace))
  6175              || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
  6176                  && valid_xwidget_spec_p (value)));
  6177 
  6178   if (valid_p && display_replaced == 0)
  6179     {
  6180       int retval = 1;
  6181 
  6182       if (!it)
  6183         {
  6184           /* Callers need to know whether the display spec is any kind
  6185              of `(space ...)' spec that is about to affect text-area
  6186              display.  */
  6187           if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
  6188             retval = 2;
  6189           return retval;
  6190         }
  6191 
  6192       /* Save current settings of IT so that we can restore them
  6193          when we are finished with the glyph property value.  */
  6194       push_it (it, position);
  6195       it->from_overlay = overlay;
  6196       it->from_disp_prop_p = true;
  6197 
  6198       if (NILP (location))
  6199         it->area = TEXT_AREA;
  6200       else if (EQ (location, Qleft_margin))
  6201         it->area = LEFT_MARGIN_AREA;
  6202       else
  6203         it->area = RIGHT_MARGIN_AREA;
  6204 
  6205       if (STRINGP (value))
  6206         {
  6207           it->string = value;
  6208           it->multibyte_p = STRING_MULTIBYTE (it->string);
  6209           it->current.overlay_string_index = -1;
  6210           IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6211           it->end_charpos = it->string_nchars = SCHARS (it->string);
  6212           it->method = GET_FROM_STRING;
  6213           it->stop_charpos = 0;
  6214           it->prev_stop = 0;
  6215           it->base_level_stop = 0;
  6216           it->string_from_display_prop_p = true;
  6217           it->cmp_it.id = -1;
  6218           /* Say that we haven't consumed the characters with
  6219              `display' property yet.  The call to pop_it in
  6220              set_iterator_to_next will clean this up.  */
  6221           if (BUFFERP (object))
  6222             *position = start_pos;
  6223 
  6224           /* Force paragraph direction to be that of the parent
  6225              object.  If the parent object's paragraph direction is
  6226              not yet determined, default to L2R.  */
  6227           if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6228             it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6229           else
  6230             it->paragraph_embedding = L2R;
  6231 
  6232           /* Set up the bidi iterator for this display string.  */
  6233           if (it->bidi_p)
  6234             {
  6235               it->bidi_it.string.lstring = it->string;
  6236               it->bidi_it.string.s = NULL;
  6237               it->bidi_it.string.schars = it->end_charpos;
  6238               it->bidi_it.string.bufpos = bufpos;
  6239               it->bidi_it.string.from_disp_str = true;
  6240               it->bidi_it.string.unibyte = !it->multibyte_p;
  6241               it->bidi_it.w = it->w;
  6242               bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6243             }
  6244         }
  6245       else if (CONSP (value) && EQ (XCAR (value), Qspace))
  6246         {
  6247           it->method = GET_FROM_STRETCH;
  6248           it->object = value;
  6249           *position = it->position = start_pos;
  6250           retval = 1 + (it->area == TEXT_AREA);
  6251         }
  6252       else if (valid_xwidget_spec_p (value))
  6253         {
  6254           it->what = IT_XWIDGET;
  6255           it->method = GET_FROM_XWIDGET;
  6256           it->position = start_pos;
  6257           it->object = NILP (object) ? it->w->contents : object;
  6258           *position = start_pos;
  6259           it->xwidget = lookup_xwidget (value);
  6260         }
  6261 #ifdef HAVE_WINDOW_SYSTEM
  6262       else
  6263         {
  6264           specpdl_ref count = SPECPDL_INDEX ();
  6265 
  6266           it->what = IT_IMAGE;
  6267           /* Don't allow quitting from lookup_image, for when we are
  6268              displaying a non-selected window, and the buffer's point
  6269              was temporarily moved to the window-point.  */
  6270           specbind (Qinhibit_quit, Qt);
  6271           it->image_id = lookup_image (it->f, value, it->face_id);
  6272           unbind_to (count, Qnil);
  6273           it->position = start_pos;
  6274           it->object = NILP (object) ? it->w->contents : object;
  6275           it->method = GET_FROM_IMAGE;
  6276 
  6277           /* Say that we haven't consumed the characters with
  6278              `display' property yet.  The call to pop_it in
  6279              set_iterator_to_next will clean this up.  */
  6280           *position = start_pos;
  6281         }
  6282 #endif /* HAVE_WINDOW_SYSTEM */
  6283 
  6284       return retval;
  6285     }
  6286 
  6287   /* Invalid property or property not supported.  Restore
  6288      POSITION to what it was before.  */
  6289   *position = start_pos;
  6290   return 0;
  6291 }
  6292 
  6293 /* Check if PROP is a display property value whose text should be
  6294    treated as intangible.  OVERLAY is the overlay from which PROP
  6295    came, or nil if it came from a text property.  CHARPOS and BYTEPOS
  6296    specify the buffer position covered by PROP.  */
  6297 
  6298 bool
  6299 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
  6300                            ptrdiff_t charpos, ptrdiff_t bytepos)
  6301 {
  6302   bool frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
  6303   struct text_pos position;
  6304 
  6305   SET_TEXT_POS (position, charpos, bytepos);
  6306   return (handle_display_spec (NULL, prop, Qnil, overlay,
  6307                                &position, charpos, frame_window_p)
  6308           != 0);
  6309 }
  6310 
  6311 
  6312 /* Return true if PROP is a display sub-property value containing STRING.
  6313 
  6314    Implementation note: this and the following function are really
  6315    special cases of handle_display_spec and
  6316    handle_single_display_spec, and should ideally use the same code.
  6317    Until they do, these two pairs must be consistent and must be
  6318    modified in sync.  */
  6319 
  6320 static bool
  6321 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
  6322 {
  6323   if (EQ (string, prop))
  6324     return true;
  6325 
  6326   /* Skip over `when FORM'.  */
  6327   if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
  6328     {
  6329       prop = XCDR (prop);
  6330       if (!CONSP (prop))
  6331         return false;
  6332       /* Actually, the condition following `when' should be eval'ed,
  6333          like handle_single_display_spec does, and we should return
  6334          false if it evaluates to nil.  However, this function is
  6335          called only when the buffer was already displayed and some
  6336          glyph in the glyph matrix was found to come from a display
  6337          string.  Therefore, the condition was already evaluated, and
  6338          the result was non-nil, otherwise the display string wouldn't
  6339          have been displayed and we would have never been called for
  6340          this property.  Thus, we can skip the evaluation and assume
  6341          its result is non-nil.  */
  6342       prop = XCDR (prop);
  6343     }
  6344 
  6345   if (CONSP (prop))
  6346     /* Skip over `margin LOCATION'.  */
  6347     if (EQ (XCAR (prop), Qmargin))
  6348       {
  6349         prop = XCDR (prop);
  6350         if (!CONSP (prop))
  6351           return false;
  6352 
  6353         prop = XCDR (prop);
  6354         if (!CONSP (prop))
  6355           return false;
  6356       }
  6357 
  6358   return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
  6359 }
  6360 
  6361 
  6362 /* Return true if STRING appears in the `display' property PROP.  */
  6363 
  6364 static bool
  6365 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
  6366 {
  6367   if (CONSP (prop)
  6368       && !EQ (XCAR (prop), Qwhen)
  6369       && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
  6370     {
  6371       /* A list of sub-properties.  */
  6372       while (CONSP (prop))
  6373         {
  6374           if (single_display_spec_string_p (XCAR (prop), string))
  6375             return true;
  6376           prop = XCDR (prop);
  6377         }
  6378     }
  6379   else if (VECTORP (prop))
  6380     {
  6381       /* A vector of sub-properties.  */
  6382       ptrdiff_t i;
  6383       for (i = 0; i < ASIZE (prop); ++i)
  6384         if (single_display_spec_string_p (AREF (prop, i), string))
  6385           return true;
  6386     }
  6387   else
  6388     return single_display_spec_string_p (prop, string);
  6389 
  6390   return false;
  6391 }
  6392 
  6393 /* Look for STRING in overlays and text properties in the current
  6394    buffer, between character positions FROM and TO (excluding TO).
  6395    BACK_P means look back (in this case, TO is supposed to be
  6396    less than FROM).
  6397    Value is the first character position where STRING was found, or
  6398    zero if it wasn't found before hitting TO.
  6399 
  6400    This function may only use code that doesn't eval because it is
  6401    called asynchronously from note_mouse_highlight.  */
  6402 
  6403 static ptrdiff_t
  6404 string_buffer_position_lim (Lisp_Object string,
  6405                             ptrdiff_t from, ptrdiff_t to, bool back_p)
  6406 {
  6407   Lisp_Object limit, prop, pos;
  6408   bool found = false;
  6409 
  6410   pos = make_fixnum (max (from, BEGV));
  6411 
  6412   if (!back_p)  /* looking forward */
  6413     {
  6414       limit = make_fixnum (min (to, ZV));
  6415       while (!found && !EQ (pos, limit))
  6416         {
  6417           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6418           if (!NILP (prop) && display_prop_string_p (prop, string))
  6419             found = true;
  6420           else
  6421             pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
  6422                                                      limit);
  6423         }
  6424     }
  6425   else          /* looking back */
  6426     {
  6427       limit = make_fixnum (max (to, BEGV));
  6428       while (!found && !EQ (pos, limit))
  6429         {
  6430           prop = Fget_char_property (pos, Qdisplay, Qnil);
  6431           if (!NILP (prop) && display_prop_string_p (prop, string))
  6432             found = true;
  6433           else
  6434             pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
  6435                                                          limit);
  6436         }
  6437     }
  6438 
  6439   return found ? XFIXNUM (pos) : 0;
  6440 }
  6441 
  6442 /* Determine which buffer position in current buffer STRING comes from.
  6443    AROUND_CHARPOS is an approximate position where it could come from.
  6444    Value is the buffer position or 0 if it couldn't be determined.
  6445 
  6446    This function is necessary because we don't record buffer positions
  6447    in glyphs generated from strings (to keep struct glyph small).
  6448    This function may only use code that doesn't eval because it is
  6449    called asynchronously from note_mouse_highlight.  */
  6450 
  6451 static ptrdiff_t
  6452 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
  6453 {
  6454   const int MAX_DISTANCE = 1000;
  6455   ptrdiff_t forward_limit = min (around_charpos + MAX_DISTANCE, ZV);
  6456   ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
  6457                                                 forward_limit, false);
  6458 
  6459   if (!found)
  6460     {
  6461       ptrdiff_t backward_limit = max (around_charpos - MAX_DISTANCE, BEGV);
  6462       found = string_buffer_position_lim (string, around_charpos,
  6463                                           backward_limit, true);
  6464     }
  6465   return found;
  6466 }
  6467 
  6468 
  6469 
  6470 /***********************************************************************
  6471                         `composition' property
  6472  ***********************************************************************/
  6473 
  6474 /* Set up iterator IT from `composition' property at its current
  6475    position.  Called from handle_stop.  */
  6476 
  6477 static enum prop_handled
  6478 handle_composition_prop (struct it *it)
  6479 {
  6480   Lisp_Object prop, string;
  6481   ptrdiff_t pos, pos_byte, start, end;
  6482 
  6483   if (STRINGP (it->string))
  6484     {
  6485       unsigned char *s;
  6486 
  6487       pos = IT_STRING_CHARPOS (*it);
  6488       pos_byte = IT_STRING_BYTEPOS (*it);
  6489       string = it->string;
  6490       s = SDATA (string) + pos_byte;
  6491       if (STRING_MULTIBYTE (string))
  6492         it->c = STRING_CHAR (s);
  6493       else
  6494         it->c = *s;
  6495     }
  6496   else
  6497     {
  6498       pos = IT_CHARPOS (*it);
  6499       pos_byte = IT_BYTEPOS (*it);
  6500       string = Qnil;
  6501       it->c = FETCH_CHAR (pos_byte);
  6502     }
  6503 
  6504   /* If there's a valid composition and point is not inside of the
  6505      composition (in the case that the composition is from the current
  6506      buffer), draw a glyph composed from the composition components.  */
  6507   if (find_composition (pos, -1, &start, &end, &prop, string)
  6508       && composition_valid_p (start, end, prop)
  6509       && (STRINGP (it->string) || (PT <= start || PT >= end)))
  6510     {
  6511       if (start < pos)
  6512         /* As we can't handle this situation (perhaps font-lock added
  6513            a new composition), we just return here hoping that next
  6514            redisplay will detect this composition much earlier.  */
  6515         return HANDLED_NORMALLY;
  6516       if (start != pos)
  6517         {
  6518           if (STRINGP (it->string))
  6519             pos_byte = string_char_to_byte (it->string, start);
  6520           else
  6521             pos_byte = CHAR_TO_BYTE (start);
  6522         }
  6523       it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
  6524                                                prop, string);
  6525 
  6526       if (it->cmp_it.id >= 0)
  6527         {
  6528           it->cmp_it.ch = -1;
  6529           it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
  6530           it->cmp_it.nglyphs = -1;
  6531         }
  6532     }
  6533 
  6534   return HANDLED_NORMALLY;
  6535 }
  6536 
  6537 
  6538 
  6539 /***********************************************************************
  6540                            Overlay strings
  6541  ***********************************************************************/
  6542 
  6543 /* The following structure is used to record overlay strings for
  6544    later sorting in load_overlay_strings.  */
  6545 
  6546 struct overlay_entry
  6547 {
  6548   Lisp_Object overlay;
  6549   Lisp_Object string;
  6550   EMACS_INT priority;
  6551   bool after_string_p;
  6552 };
  6553 
  6554 
  6555 /* Set up iterator IT from overlay strings at its current position.
  6556    Called from handle_stop.  */
  6557 
  6558 static enum prop_handled
  6559 handle_overlay_change (struct it *it)
  6560 {
  6561   if (!STRINGP (it->string) && get_overlay_strings (it, 0))
  6562     return HANDLED_RECOMPUTE_PROPS;
  6563   else
  6564     return HANDLED_NORMALLY;
  6565 }
  6566 
  6567 
  6568 /* Set up the next overlay string for delivery by IT, if there is an
  6569    overlay string to deliver.  Called by set_iterator_to_next when the
  6570    end of the current overlay string is reached.  If there are more
  6571    overlay strings to display, IT->string and
  6572    IT->current.overlay_string_index are set appropriately here.
  6573    Otherwise IT->string is set to nil.  */
  6574 
  6575 static void
  6576 next_overlay_string (struct it *it)
  6577 {
  6578   ++it->current.overlay_string_index;
  6579   if (it->current.overlay_string_index == it->n_overlay_strings)
  6580     {
  6581       /* No more overlay strings.  Restore IT's settings to what
  6582          they were before overlay strings were processed, and
  6583          continue to deliver from current_buffer.  */
  6584 
  6585       it->ellipsis_p = it->stack[it->sp - 1].display_ellipsis_p;
  6586       pop_it (it);
  6587       eassert (it->sp > 0
  6588                || (NILP (it->string)
  6589                    && it->method == GET_FROM_BUFFER
  6590                    && it->stop_charpos >= BEGV
  6591                    && it->stop_charpos <= it->end_charpos));
  6592       it->current.overlay_string_index = -1;
  6593       it->n_overlay_strings = 0;
  6594       /* If there's an empty display string on the stack, pop the
  6595          stack, to resync the bidi iterator with IT's position.  Such
  6596          empty strings are pushed onto the stack in
  6597          get_overlay_strings_1.  */
  6598       if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
  6599         pop_it (it);
  6600 
  6601       /* Since we've exhausted overlay strings at this buffer
  6602          position, set the flag to ignore overlays until we move to
  6603          another position.  (The flag will be reset in
  6604          next_element_from_buffer.)  But don't do that if the overlay
  6605          strings were loaded at position other than the current one,
  6606          which could happen if we called pop_it above, or if the
  6607          overlay strings were loaded by handle_invisible_prop at the
  6608          beginning of invisible text.  */
  6609       if (it->overlay_strings_charpos == IT_CHARPOS (*it))
  6610         it->ignore_overlay_strings_at_pos_p = true;
  6611 
  6612       /* If we're at the end of the buffer, record that we have
  6613          processed the overlay strings there already, so that
  6614          next_element_from_buffer doesn't try it again.  */
  6615       if (NILP (it->string)
  6616           && IT_CHARPOS (*it) >= it->end_charpos
  6617           && it->overlay_strings_charpos >= it->end_charpos)
  6618         it->overlay_strings_at_end_processed_p = true;
  6619       /* Note: we reset overlay_strings_charpos only here, to make
  6620          sure the just-processed overlays were indeed at EOB.
  6621          Otherwise, overlays on text with invisible text property,
  6622          which are processed with IT's position past the invisible
  6623          text, might fool us into thinking the overlays at EOB were
  6624          already processed (linum-mode can cause this, for
  6625          example).  */
  6626       it->overlay_strings_charpos = -1;
  6627     }
  6628   else
  6629     {
  6630       /* There are more overlay strings to process.  If
  6631          IT->current.overlay_string_index has advanced to a position
  6632          where we must load IT->overlay_strings with more strings, do
  6633          it.  We must load at the IT->overlay_strings_charpos where
  6634          IT->n_overlay_strings was originally computed; when invisible
  6635          text is present, this might not be IT_CHARPOS (Bug#7016).  */
  6636       int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
  6637 
  6638       if (it->current.overlay_string_index && i == 0)
  6639         load_overlay_strings (it, it->overlay_strings_charpos);
  6640 
  6641       /* Initialize IT to deliver display elements from the overlay
  6642          string.  */
  6643       it->string = it->overlay_strings[i];
  6644       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6645       SET_TEXT_POS (it->current.string_pos, 0, 0);
  6646       it->method = GET_FROM_STRING;
  6647       it->stop_charpos = 0;
  6648       it->end_charpos = SCHARS (it->string);
  6649       if (it->cmp_it.stop_pos >= 0)
  6650         it->cmp_it.stop_pos = 0;
  6651       it->prev_stop = 0;
  6652       it->base_level_stop = 0;
  6653 
  6654       /* Set up the bidi iterator for this overlay string.  */
  6655       if (it->bidi_p)
  6656         {
  6657           it->bidi_it.string.lstring = it->string;
  6658           it->bidi_it.string.s = NULL;
  6659           it->bidi_it.string.schars = SCHARS (it->string);
  6660           it->bidi_it.string.bufpos = it->overlay_strings_charpos;
  6661           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6662           it->bidi_it.string.unibyte = !it->multibyte_p;
  6663           it->bidi_it.w = it->w;
  6664           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6665         }
  6666     }
  6667 
  6668   CHECK_IT (it);
  6669 }
  6670 
  6671 
  6672 /* Compare two overlay_entry structures E1 and E2.  Used as a
  6673    comparison function for qsort in load_overlay_strings.  Overlay
  6674    strings for the same position are sorted so that
  6675 
  6676    1. All after-strings come in front of before-strings, except
  6677    when they come from the same overlay.
  6678 
  6679    2. Within after-strings, strings are sorted so that overlay strings
  6680    from overlays with higher priorities come first.
  6681 
  6682    2. Within before-strings, strings are sorted so that overlay
  6683    strings from overlays with higher priorities come last.
  6684 
  6685    Value is analogous to strcmp.  */
  6686 
  6687 
  6688 static int
  6689 compare_overlay_entries (const void *e1, const void *e2)
  6690 {
  6691   struct overlay_entry const *entry1 = e1;
  6692   struct overlay_entry const *entry2 = e2;
  6693   int result;
  6694 
  6695   if (entry1->after_string_p != entry2->after_string_p)
  6696     {
  6697       /* Let after-strings appear in front of before-strings if
  6698          they come from different overlays.  */
  6699       if (EQ (entry1->overlay, entry2->overlay))
  6700         result = entry1->after_string_p ? 1 : -1;
  6701       else
  6702         result = entry1->after_string_p ? -1 : 1;
  6703     }
  6704   else if (entry1->priority != entry2->priority)
  6705     {
  6706       if (entry1->after_string_p)
  6707         /* After-strings sorted in order of decreasing priority.  */
  6708         result = entry2->priority < entry1->priority ? -1 : 1;
  6709       else
  6710         /* Before-strings sorted in order of increasing priority.  */
  6711         result = entry1->priority < entry2->priority ? -1 : 1;
  6712     }
  6713   else
  6714     result = 0;
  6715 
  6716   return result;
  6717 }
  6718 
  6719 
  6720 /* Load the vector IT->overlay_strings with overlay strings from IT's
  6721    current buffer position, or from CHARPOS if that is > 0.  Set
  6722    IT->n_overlays to the total number of overlay strings found.
  6723 
  6724    Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
  6725    a time.  On entry into load_overlay_strings,
  6726    IT->current.overlay_string_index gives the number of overlay
  6727    strings that have already been loaded by previous calls to this
  6728    function.
  6729 
  6730    IT->add_overlay_start contains an additional overlay start
  6731    position to consider for taking overlay strings from, if non-zero.
  6732    This position comes into play when the overlay has an `invisible'
  6733    property, and both before and after-strings.  When we've skipped to
  6734    the end of the overlay, because of its `invisible' property, we
  6735    nevertheless want its before-string to appear.
  6736    IT->add_overlay_start will contain the overlay start position
  6737    in this case.
  6738 
  6739    Overlay strings are sorted so that after-string strings come in
  6740    front of before-string strings.  Within before and after-strings,
  6741    strings are sorted by overlay priority.  See also function
  6742    compare_overlay_entries.  */
  6743 
  6744 static void
  6745 load_overlay_strings (struct it *it, ptrdiff_t charpos)
  6746 {
  6747   ptrdiff_t n = 0;
  6748   struct overlay_entry entriesbuf[20];
  6749   ptrdiff_t size = ARRAYELTS (entriesbuf);
  6750   struct overlay_entry *entries = entriesbuf;
  6751   struct itree_node *node;
  6752 
  6753   USE_SAFE_ALLOCA;
  6754 
  6755   if (charpos <= 0)
  6756     charpos = IT_CHARPOS (*it);
  6757 
  6758   /* Append the overlay string STRING of overlay OVERLAY to vector
  6759      `entries' which has size `size' and currently contains `n'
  6760      elements.  AFTER_P means STRING is an after-string of
  6761      OVERLAY.  */
  6762 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P)                 \
  6763   do                                                                    \
  6764     {                                                                   \
  6765       Lisp_Object priority;                                             \
  6766                                                                         \
  6767       if (n == size)                                                    \
  6768         {                                                               \
  6769           struct overlay_entry *old = entries;                          \
  6770           SAFE_NALLOCA (entries, 2, size);                              \
  6771           memcpy (entries, old, size * sizeof *entries);                \
  6772           size *= 2;                                                    \
  6773         }                                                               \
  6774                                                                         \
  6775       entries[n].string = (STRING);                                     \
  6776       entries[n].overlay = (OVERLAY);                                   \
  6777       priority = Foverlay_get ((OVERLAY), Qpriority);                   \
  6778       entries[n].priority = FIXNUMP (priority) ? XFIXNUM (priority) : 0;  \
  6779       entries[n].after_string_p = (AFTER_P);                            \
  6780       ++n;                                                              \
  6781     }                                                                   \
  6782   while (false)
  6783 
  6784 
  6785   /* Process overlays.  */
  6786   ITREE_FOREACH (node, current_buffer->overlays, charpos - 1, charpos + 1, DESCENDING)
  6787     {
  6788       Lisp_Object overlay = node->data;
  6789       eassert (OVERLAYP (overlay));
  6790       ptrdiff_t start = node->begin;
  6791       ptrdiff_t end = node->end;
  6792 
  6793       /* Skip this overlay if it doesn't start or end at IT's current
  6794          position.  */
  6795       if (end != charpos && start != charpos)
  6796         continue;
  6797 
  6798       /* Skip this overlay if it doesn't apply to IT->w.  */
  6799       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  6800       if (WINDOWP (window) && XWINDOW (window) != it->w)
  6801         continue;
  6802 
  6803       /* If the text ``under'' the overlay is invisible, both before-
  6804          and after-strings from this overlay are visible; start and
  6805          end position are indistinguishable.  */
  6806       Lisp_Object invisible = Foverlay_get (overlay, Qinvisible);
  6807       int invis = TEXT_PROP_MEANS_INVISIBLE (invisible);
  6808 
  6809       /* If overlay has a non-empty before-string, record it.  */
  6810       Lisp_Object str;
  6811       if ((start == charpos || (end == charpos && invis != 0))
  6812           && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
  6813           && SCHARS (str))
  6814         RECORD_OVERLAY_STRING (overlay, str, false);
  6815 
  6816       /* If overlay has a non-empty after-string, record it.  */
  6817       if ((end == charpos || (start == charpos && invis != 0))
  6818           && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
  6819           && SCHARS (str))
  6820         RECORD_OVERLAY_STRING (overlay, str, true);
  6821     }
  6822 
  6823 #undef RECORD_OVERLAY_STRING
  6824 
  6825   /* Sort entries.  */
  6826   if (n > 1)
  6827     qsort (entries, n, sizeof *entries, compare_overlay_entries);
  6828 
  6829   /* Record number of overlay strings, and where we computed it.  */
  6830   it->n_overlay_strings = n;
  6831   it->overlay_strings_charpos = charpos;
  6832 
  6833   /* IT->current.overlay_string_index is the number of overlay strings
  6834      that have already been consumed by IT.  Copy some of the
  6835      remaining overlay strings to IT->overlay_strings.  */
  6836   ptrdiff_t j = it->current.overlay_string_index;
  6837   for (ptrdiff_t i = 0; i < OVERLAY_STRING_CHUNK_SIZE && j < n; i++, j++)
  6838     {
  6839       it->overlay_strings[i] = entries[j].string;
  6840       it->string_overlays[i] = entries[j].overlay;
  6841     }
  6842 
  6843   CHECK_IT (it);
  6844   SAFE_FREE ();
  6845 }
  6846 
  6847 
  6848 /* Get the first chunk of overlay strings at IT's current buffer
  6849    position, or at CHARPOS if that is > 0.  Value is true if at
  6850    least one overlay string was found.  */
  6851 
  6852 static bool
  6853 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, bool compute_stop_p)
  6854 {
  6855   /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
  6856      process.  This fills IT->overlay_strings with strings, and sets
  6857      IT->n_overlay_strings to the total number of strings to process.
  6858      IT->pos.overlay_string_index has to be set temporarily to zero
  6859      because load_overlay_strings needs this; it must be set to -1
  6860      when no overlay strings are found because a zero value would
  6861      indicate a position in the first overlay string.  */
  6862   it->current.overlay_string_index = 0;
  6863   load_overlay_strings (it, charpos);
  6864 
  6865   /* If we found overlay strings, set up IT to deliver display
  6866      elements from the first one.  Otherwise set up IT to deliver
  6867      from current_buffer.  */
  6868   if (it->n_overlay_strings)
  6869     {
  6870       /* Make sure we know settings in current_buffer, so that we can
  6871          restore meaningful values when we're done with the overlay
  6872          strings.  */
  6873       if (compute_stop_p)
  6874         compute_stop_pos (it);
  6875       eassert (it->face_id >= 0);
  6876 
  6877       /* Save IT's settings.  They are restored after all overlay
  6878          strings have been processed.  */
  6879       eassert (!compute_stop_p || it->sp == 0);
  6880 
  6881       /* When called from handle_stop, there might be an empty display
  6882          string loaded.  In that case, don't bother saving it.  But
  6883          don't use this optimization with the bidi iterator, since we
  6884          need the corresponding pop_it call to resync the bidi
  6885          iterator's position with IT's position, after we are done
  6886          with the overlay strings.  (The corresponding call to pop_it
  6887          in case of an empty display string is in
  6888          next_overlay_string.)  */
  6889       if (!(!it->bidi_p
  6890             && STRINGP (it->string) && !SCHARS (it->string)))
  6891         push_it (it, NULL);
  6892 
  6893       /* Set up IT to deliver display elements from the first overlay
  6894          string.  */
  6895       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
  6896       it->string = it->overlay_strings[0];
  6897       it->from_overlay = Qnil;
  6898       it->stop_charpos = 0;
  6899       eassert (STRINGP (it->string));
  6900       it->end_charpos = SCHARS (it->string);
  6901       it->prev_stop = 0;
  6902       it->base_level_stop = 0;
  6903       it->multibyte_p = STRING_MULTIBYTE (it->string);
  6904       it->method = GET_FROM_STRING;
  6905       it->from_disp_prop_p = 0;
  6906       it->cmp_it.id = -1;
  6907 
  6908       /* Force paragraph direction to be that of the parent
  6909          buffer.  */
  6910       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
  6911         it->paragraph_embedding = it->bidi_it.paragraph_dir;
  6912       else
  6913         it->paragraph_embedding = L2R;
  6914 
  6915       /* Set up the bidi iterator for this overlay string.  */
  6916       if (it->bidi_p)
  6917         {
  6918           ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
  6919 
  6920           it->bidi_it.string.lstring = it->string;
  6921           it->bidi_it.string.s = NULL;
  6922           it->bidi_it.string.schars = SCHARS (it->string);
  6923           it->bidi_it.string.bufpos = pos;
  6924           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
  6925           it->bidi_it.string.unibyte = !it->multibyte_p;
  6926           it->bidi_it.w = it->w;
  6927           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
  6928         }
  6929       return true;
  6930     }
  6931 
  6932   it->current.overlay_string_index = -1;
  6933   return false;
  6934 }
  6935 
  6936 static bool
  6937 get_overlay_strings (struct it *it, ptrdiff_t charpos)
  6938 {
  6939   it->string = Qnil;
  6940   it->method = GET_FROM_BUFFER;
  6941 
  6942   get_overlay_strings_1 (it, charpos, true);
  6943 
  6944   CHECK_IT (it);
  6945 
  6946   /* Value is true if we found at least one overlay string.  */
  6947   return STRINGP (it->string);
  6948 }
  6949 
  6950 
  6951 
  6952 /***********************************************************************
  6953                       Saving and restoring state
  6954  ***********************************************************************/
  6955 
  6956 /* Save current settings of IT on IT->stack.  Called, for example,
  6957    before setting up IT for an overlay string, to be able to restore
  6958    IT's settings to what they were after the overlay string has been
  6959    processed.  If POSITION is non-NULL, it is the position to save on
  6960    the stack instead of IT->position.  */
  6961 
  6962 static void
  6963 push_it (struct it *it, struct text_pos *position)
  6964 {
  6965   struct iterator_stack_entry *p;
  6966 
  6967   eassert (it->sp < IT_STACK_SIZE);
  6968   p = it->stack + it->sp;
  6969 
  6970   p->stop_charpos = it->stop_charpos;
  6971   p->prev_stop = it->prev_stop;
  6972   p->base_level_stop = it->base_level_stop;
  6973   p->cmp_it = it->cmp_it;
  6974   eassert (it->face_id >= 0);
  6975   p->face_id = it->face_id;
  6976   p->string = it->string;
  6977   p->method = it->method;
  6978   p->from_overlay = it->from_overlay;
  6979   switch (p->method)
  6980     {
  6981     case GET_FROM_IMAGE:
  6982       p->u.image.object = it->object;
  6983       p->u.image.image_id = it->image_id;
  6984       p->u.image.slice = it->slice;
  6985       break;
  6986     case GET_FROM_STRETCH:
  6987       p->u.stretch.object = it->object;
  6988       break;
  6989     case GET_FROM_XWIDGET:
  6990       p->u.xwidget.object = it->object;
  6991       break;
  6992     case GET_FROM_BUFFER:
  6993     case GET_FROM_DISPLAY_VECTOR:
  6994     case GET_FROM_STRING:
  6995     case GET_FROM_C_STRING:
  6996       break;
  6997     default:
  6998       emacs_abort ();
  6999     }
  7000   p->position = position ? *position : it->position;
  7001   p->current = it->current;
  7002   p->end_charpos = it->end_charpos;
  7003   p->string_nchars = it->string_nchars;
  7004   p->area = it->area;
  7005   p->multibyte_p = it->multibyte_p;
  7006   p->avoid_cursor_p = it->avoid_cursor_p;
  7007   p->space_width = it->space_width;
  7008   p->font_height = it->font_height;
  7009   p->voffset = it->voffset;
  7010   p->string_from_display_prop_p = it->string_from_display_prop_p;
  7011   p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
  7012   p->display_ellipsis_p = false;
  7013   p->line_wrap = it->line_wrap;
  7014   p->bidi_p = it->bidi_p;
  7015   p->paragraph_embedding = it->paragraph_embedding;
  7016   p->from_disp_prop_p = it->from_disp_prop_p;
  7017   ++it->sp;
  7018 
  7019   /* Save the state of the bidi iterator as well. */
  7020   if (it->bidi_p)
  7021     bidi_push_it (&it->bidi_it);
  7022 }
  7023 
  7024 static void
  7025 iterate_out_of_display_property (struct it *it)
  7026 {
  7027   bool buffer_p = !STRINGP (it->string);
  7028   ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
  7029   ptrdiff_t bob = (buffer_p ? BEGV : 0);
  7030 
  7031   eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
  7032 
  7033   /* Maybe initialize paragraph direction.  If we are at the beginning
  7034      of a new paragraph, next_element_from_buffer may not have a
  7035      chance to do that.  */
  7036   if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
  7037     bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  7038   /* prev_stop can be zero, so check against BEGV as well.  */
  7039   while (it->bidi_it.charpos >= bob
  7040          && it->prev_stop <= it->bidi_it.charpos
  7041          && it->bidi_it.charpos < CHARPOS (it->position)
  7042          && it->bidi_it.charpos < eob)
  7043     bidi_move_to_visually_next (&it->bidi_it);
  7044   /* Record the stop_pos we just crossed, for when we cross it
  7045      back, maybe.  */
  7046   if (it->bidi_it.charpos > CHARPOS (it->position))
  7047     it->prev_stop = CHARPOS (it->position);
  7048   /* If we ended up not where pop_it put us, resync IT's
  7049      positional members with the bidi iterator. */
  7050   if (it->bidi_it.charpos != CHARPOS (it->position))
  7051     SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
  7052   if (buffer_p)
  7053     it->current.pos = it->position;
  7054   else
  7055     it->current.string_pos = it->position;
  7056 }
  7057 
  7058 /* Restore the IT->face_box_p flag, since it could have been
  7059    overwritten by the face of the object that we just finished
  7060    displaying.  Also, set the IT->start_of_box_run_p flag if the
  7061    change in faces requires that.  */
  7062 static void
  7063 restore_face_box_flags (struct it *it, int prev_face_id)
  7064 {
  7065   struct face *face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  7066 
  7067   if (face)
  7068     {
  7069       struct face *prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  7070 
  7071       if (!(it->start_of_box_run_p && prev_face && prev_face->box))
  7072         it->start_of_box_run_p = (face->box != FACE_NO_BOX
  7073                                   && (prev_face == NULL
  7074                                       || prev_face->box == FACE_NO_BOX));
  7075       it->face_box_p = face->box != FACE_NO_BOX;
  7076     }
  7077 }
  7078 
  7079 /* Restore IT's settings from IT->stack.  Called, for example, when no
  7080    more overlay strings must be processed, and we return to delivering
  7081    display elements from a buffer, or when the end of a string from a
  7082    `display' property is reached and we return to delivering display
  7083    elements from an overlay string, or from a buffer.  */
  7084 
  7085 static void
  7086 pop_it (struct it *it)
  7087 {
  7088   struct iterator_stack_entry *p;
  7089   bool from_display_prop = it->from_disp_prop_p;
  7090   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  7091   int prev_face_id = it->face_id;
  7092 
  7093   eassert (it->sp > 0);
  7094   --it->sp;
  7095   p = it->stack + it->sp;
  7096   it->stop_charpos = p->stop_charpos;
  7097   it->prev_stop = p->prev_stop;
  7098   it->base_level_stop = p->base_level_stop;
  7099   it->cmp_it = p->cmp_it;
  7100   it->face_id = p->face_id;
  7101   it->current = p->current;
  7102   it->position = p->position;
  7103   it->string = p->string;
  7104   it->from_overlay = p->from_overlay;
  7105   if (NILP (it->string))
  7106     SET_TEXT_POS (it->current.string_pos, -1, -1);
  7107   it->method = p->method;
  7108   switch (it->method)
  7109     {
  7110     case GET_FROM_IMAGE:
  7111       it->image_id = p->u.image.image_id;
  7112       it->object = p->u.image.object;
  7113       it->slice = p->u.image.slice;
  7114       break;
  7115     case GET_FROM_XWIDGET:
  7116       it->object = p->u.xwidget.object;
  7117       break;
  7118     case GET_FROM_STRETCH:
  7119       it->object = p->u.stretch.object;
  7120       break;
  7121     case GET_FROM_BUFFER:
  7122       {
  7123         restore_face_box_flags (it, prev_face_id);
  7124         it->object = it->w->contents;
  7125       }
  7126       break;
  7127     case GET_FROM_STRING:
  7128       {
  7129         restore_face_box_flags (it, prev_face_id);
  7130         it->object = it->string;
  7131       }
  7132       break;
  7133     case GET_FROM_DISPLAY_VECTOR:
  7134       if (it->s)
  7135         it->method = GET_FROM_C_STRING;
  7136       else if (STRINGP (it->string))
  7137         it->method = GET_FROM_STRING;
  7138       else
  7139         {
  7140           it->method = GET_FROM_BUFFER;
  7141           it->object = it->w->contents;
  7142         }
  7143       break;
  7144     case GET_FROM_C_STRING:
  7145       break;
  7146     default:
  7147       emacs_abort ();
  7148     }
  7149   it->end_charpos = p->end_charpos;
  7150   it->string_nchars = p->string_nchars;
  7151   it->area = p->area;
  7152   it->multibyte_p = p->multibyte_p;
  7153   it->avoid_cursor_p = p->avoid_cursor_p;
  7154   it->space_width = p->space_width;
  7155   it->font_height = p->font_height;
  7156   it->voffset = p->voffset;
  7157   it->string_from_display_prop_p = p->string_from_display_prop_p;
  7158   it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
  7159   it->line_wrap = p->line_wrap;
  7160   it->bidi_p = p->bidi_p;
  7161   it->paragraph_embedding = p->paragraph_embedding;
  7162   it->from_disp_prop_p = p->from_disp_prop_p;
  7163   if (it->bidi_p)
  7164     {
  7165       bidi_pop_it (&it->bidi_it);
  7166       /* Bidi-iterate until we get out of the portion of text, if any,
  7167          covered by a `display' text property or by an overlay with
  7168          `display' property.  (We cannot just jump there, because the
  7169          internal coherency of the bidi iterator state can not be
  7170          preserved across such jumps.)  We also must determine the
  7171          paragraph base direction if the overlay we just processed is
  7172          at the beginning of a new paragraph.  */
  7173       if (from_display_prop
  7174           && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
  7175         iterate_out_of_display_property (it);
  7176 
  7177       eassert ((BUFFERP (it->object)
  7178                 && IT_CHARPOS (*it) == it->bidi_it.charpos
  7179                 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
  7180                || (STRINGP (it->object)
  7181                    && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
  7182                    && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
  7183                || (CONSP (it->object) && it->method == GET_FROM_STRETCH)
  7184                /* We could be in the middle of handling a list or a
  7185                   vector of several 'display' properties, in which
  7186                   case we should only verify the above conditions when
  7187                   we pop the iterator stack the last time, because
  7188                   higher stack levels cannot "iterate out of the
  7189                   display property".  */
  7190                || it->sp > 0);
  7191     }
  7192   /* If we move the iterator over text covered by a display property
  7193      to a new buffer position, any info about previously seen overlays
  7194      is no longer valid.  */
  7195   if (from_display_prop && it->sp == 0 && CHARPOS (it->position) != prev_pos)
  7196     it->ignore_overlay_strings_at_pos_p = false;
  7197 }
  7198 
  7199 
  7200 
  7201 /***********************************************************************
  7202                           Moving over lines
  7203  ***********************************************************************/
  7204 
  7205 /* Set IT's current position to the previous line start.  */
  7206 
  7207 static void
  7208 back_to_previous_line_start (struct it *it)
  7209 {
  7210   ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
  7211 
  7212   dec_both (&cp, &bp);
  7213   SET_WITH_NARROWED_BEGV (it, IT_CHARPOS (*it),
  7214                           find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it)),
  7215                           get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
  7216 }
  7217 
  7218 /* Find in the current buffer the first display or overlay string
  7219    between STARTPOS and ENDPOS that includes embedded newlines.
  7220    Consider only overlays that apply to window W.
  7221    Value is non-zero if such a display/overlay string is found.  */
  7222 static bool
  7223 strings_with_newlines (ptrdiff_t startpos, ptrdiff_t endpos, struct window *w)
  7224 {
  7225   struct itree_node *node;
  7226   /* Process overlays.  */
  7227   ITREE_FOREACH (node, current_buffer->overlays, startpos, endpos, DESCENDING)
  7228     {
  7229       Lisp_Object overlay = node->data;
  7230       eassert (OVERLAYP (overlay));
  7231 
  7232       /* Skip this overlay if it doesn't apply to our window.  */
  7233       Lisp_Object window = Foverlay_get (overlay, Qwindow);
  7234       if (WINDOWP (window) && XWINDOW (window) != w)
  7235         continue;
  7236 
  7237       ptrdiff_t ostart = node->begin;
  7238       ptrdiff_t oend = node->end;
  7239 
  7240       /* Skip overlays that don't overlap the range.  */
  7241       if (!((startpos < oend && ostart < endpos)
  7242             || (ostart == oend
  7243                 && (startpos == oend || (endpos == ZV && oend == endpos)))))
  7244         continue;
  7245 
  7246       Lisp_Object str;
  7247       str = Foverlay_get (overlay, Qbefore_string);
  7248       if (STRINGP (str) && SCHARS (str)
  7249           && memchr (SDATA (str), '\n', SBYTES (str)))
  7250         return true;
  7251       str = Foverlay_get (overlay, Qafter_string);
  7252       if (STRINGP (str) && SCHARS (str)
  7253           && memchr (SDATA (str), '\n', SBYTES (str)))
  7254         return true;
  7255     }
  7256 
  7257   /* Check for 'display' properties whose values include strings.  */
  7258   Lisp_Object cpos = make_fixnum (startpos);
  7259   Lisp_Object limpos = make_fixnum (endpos);
  7260 
  7261   while ((cpos = Fnext_single_property_change (cpos, Qdisplay, Qnil, limpos),
  7262           !(NILP (cpos) || XFIXNAT (cpos) >= endpos)))
  7263     {
  7264       Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
  7265       Lisp_Object string = string_from_display_spec (spec);
  7266       if (STRINGP (string)
  7267           && memchr (SDATA (string), '\n', SBYTES (string)))
  7268         return true;
  7269     }
  7270 
  7271   return false;
  7272 }
  7273 
  7274 
  7275 /* Move IT to the next line start.
  7276 
  7277    Value is true if a newline was found.  Set *SKIPPED_P to true if
  7278    we skipped over part of the text (as opposed to moving the iterator
  7279    continuously over the text).  Otherwise, don't change the value
  7280    of *SKIPPED_P.
  7281 
  7282    If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
  7283    iterator on the newline, if it was found.
  7284 
  7285    Newlines may come from buffer text, overlay strings, or strings
  7286    displayed via the `display' property.  That's the reason we can't
  7287    simply use find_newline_no_quit.
  7288 
  7289    Note that this function may not skip over invisible text that is so
  7290    because of text properties and immediately follows a newline.  If
  7291    it would, function reseat_at_next_visible_line_start, when called
  7292    from set_iterator_to_next, would effectively make invisible
  7293    characters following a newline part of the wrong glyph row, which
  7294    leads to wrong cursor motion.  */
  7295 
  7296 static bool
  7297 forward_to_next_line_start (struct it *it, bool *skipped_p,
  7298                             struct bidi_it *bidi_it_prev)
  7299 {
  7300   ptrdiff_t old_selective;
  7301   bool newline_found_p = false;
  7302   int n;
  7303   const int MAX_NEWLINE_DISTANCE = 500;
  7304 
  7305   /* If already on a newline, just consume it to avoid unintended
  7306      skipping over invisible text below.  */
  7307   if (it->what == IT_CHARACTER
  7308       && it->c == '\n'
  7309       && CHARPOS (it->position) == IT_CHARPOS (*it))
  7310     {
  7311       if (it->bidi_p && bidi_it_prev)
  7312         *bidi_it_prev = it->bidi_it;
  7313       set_iterator_to_next (it, false);
  7314       it->c = 0;
  7315       return true;
  7316     }
  7317 
  7318   /* Don't handle selective display in the following.  It's (a)
  7319      unnecessary because it's done by the caller, and (b) leads to an
  7320      infinite recursion because next_element_from_ellipsis indirectly
  7321      calls this function.  */
  7322   old_selective = it->selective;
  7323   it->selective = 0;
  7324 
  7325   /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
  7326      from buffer text, or till the end of the string if iterating a
  7327      string.  */
  7328   for (n = 0;
  7329        !newline_found_p && n < MAX_NEWLINE_DISTANCE;
  7330        n += !STRINGP (it->string))
  7331     {
  7332       if (!get_next_display_element (it))
  7333         return false;
  7334       newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
  7335       if (newline_found_p && it->bidi_p && bidi_it_prev)
  7336         *bidi_it_prev = it->bidi_it;
  7337       set_iterator_to_next (it, false);
  7338     }
  7339 
  7340   /* If we didn't find a newline near enough, see if we can use a
  7341      short-cut.  */
  7342   if (!newline_found_p)
  7343     {
  7344       ptrdiff_t bytepos, start = IT_CHARPOS (*it);
  7345       ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
  7346                                               1, &bytepos);
  7347       eassert (!STRINGP (it->string));
  7348 
  7349       /* it->stop_charpos >= limit means we already know there's no
  7350          stop position up until the newline at LIMIT, so there's no
  7351          need for any further checks.  */
  7352       bool no_strings_with_newlines = it->stop_charpos >= limit;
  7353 
  7354       if (!no_strings_with_newlines)
  7355         {
  7356           if (!(current_buffer->long_line_optimizations_p
  7357                 && it->line_wrap == TRUNCATE))
  7358             {
  7359               /* Quick-and-dirty check: if there isn't any `display'
  7360                  property in sight, and no overlays, we're done.  */
  7361               Lisp_Object pos =
  7362                 Fnext_single_property_change (make_fixnum (start),
  7363                                               Qdisplay, Qnil,
  7364                                               make_fixnum (limit));
  7365               no_strings_with_newlines =
  7366                 (NILP (pos) || XFIXNAT (pos) == limit) /* no 'display' props */
  7367                 && next_overlay_change (start) == ZV;  /* no overlays */
  7368             }
  7369           else
  7370             {
  7371               /* For buffers with very long and truncated lines we try
  7372                  harder, because it's worth our while to spend some
  7373                  time looking into the overlays and 'display' properties
  7374                  if we can then avoid iterating through all of them.  */
  7375               no_strings_with_newlines =
  7376                 !strings_with_newlines (start, limit, it->w);
  7377             }
  7378         }
  7379 
  7380       /* If there's no display or overlay strings with embedded
  7381          newlines until the position of the newline in buffer text, we
  7382          can just use that position.  */
  7383       if (no_strings_with_newlines)
  7384         {
  7385           if (!it->bidi_p || !bidi_it_prev)
  7386             {
  7387               /* The optimal case: just jump there.  */
  7388               IT_CHARPOS (*it) = limit;
  7389               IT_BYTEPOS (*it) = bytepos;
  7390             }
  7391           else
  7392             {
  7393               /* The less optimal case: need to bidi-walk there, but
  7394                  this is still cheaper that the full iteration using
  7395                  get_next_display_element and set_iterator_to_next.  */
  7396               struct bidi_it bprev;
  7397 
  7398               /* Help bidi.c avoid expensive searches for display
  7399                  properties and overlays, by telling it that there are
  7400                  none up to `limit'.  */
  7401               if (it->bidi_it.disp_pos < limit)
  7402                 {
  7403                   it->bidi_it.disp_pos = limit;
  7404                   it->bidi_it.disp_prop = 0;
  7405                 }
  7406               do {
  7407                 bprev = it->bidi_it;
  7408                 bidi_move_to_visually_next (&it->bidi_it);
  7409               } while (it->bidi_it.charpos != limit);
  7410               IT_CHARPOS (*it) = limit;
  7411               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7412               if (bidi_it_prev)
  7413                 *bidi_it_prev = bprev;
  7414             }
  7415           *skipped_p = newline_found_p = true;
  7416         }
  7417       else
  7418         {
  7419           /* The slow case.  */
  7420           while (!newline_found_p)
  7421             {
  7422               if (!get_next_display_element (it))
  7423                 break;
  7424               newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
  7425               if (newline_found_p && it->bidi_p && bidi_it_prev)
  7426                 *bidi_it_prev = it->bidi_it;
  7427               set_iterator_to_next (it, false);
  7428             }
  7429         }
  7430     }
  7431 
  7432   it->selective = old_selective;
  7433   return newline_found_p;
  7434 }
  7435 
  7436 
  7437 /* Set IT's current position to the previous visible line start.  Skip
  7438    invisible text that is so either due to text properties or due to
  7439    selective display.  Caution: this does not change IT->current_x and
  7440    IT->hpos.  */
  7441 
  7442 static void
  7443 back_to_previous_visible_line_start (struct it *it)
  7444 {
  7445   while (IT_CHARPOS (*it) > BEGV)
  7446     {
  7447       back_to_previous_line_start (it);
  7448 
  7449       if (IT_CHARPOS (*it) <= BEGV)
  7450         break;
  7451 
  7452       /* If selective > 0, then lines indented more than its value are
  7453          invisible.  */
  7454       if (it->selective > 0
  7455           && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7456                                 it->selective))
  7457         continue;
  7458 
  7459       /* Check the newline before point for invisibility.  */
  7460       {
  7461         Lisp_Object prop;
  7462         prop = Fget_char_property (make_fixnum (IT_CHARPOS (*it) - 1),
  7463                                    Qinvisible, it->window);
  7464         if (TEXT_PROP_MEANS_INVISIBLE (prop) != 0)
  7465           continue;
  7466       }
  7467 
  7468       if (IT_CHARPOS (*it) <= BEGV)
  7469         break;
  7470 
  7471       {
  7472         struct it it2;
  7473         void *it2data = NULL;
  7474         ptrdiff_t pos;
  7475         ptrdiff_t beg, end;
  7476         Lisp_Object val, overlay;
  7477 
  7478         SAVE_IT (it2, *it, it2data);
  7479 
  7480         /* If newline is part of a composition, continue from start of composition */
  7481         if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
  7482             && beg < IT_CHARPOS (*it))
  7483           goto replaced;
  7484 
  7485         /* If newline is replaced by a display property, find start of overlay
  7486            or interval and continue search from that point.  */
  7487         pos = --IT_CHARPOS (it2);
  7488         --IT_BYTEPOS (it2);
  7489         it2.sp = 0;
  7490         bidi_unshelve_cache (NULL, false);
  7491         it2.string_from_display_prop_p = false;
  7492         it2.from_disp_prop_p = false;
  7493         if (handle_display_prop (&it2) == HANDLED_RETURN
  7494             && !NILP (val = get_char_property_and_overlay
  7495                       (make_fixnum (pos), Qdisplay, Qnil, &overlay))
  7496             && (OVERLAYP (overlay)
  7497                 ? (beg = OVERLAY_START (overlay))
  7498                 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
  7499           {
  7500             RESTORE_IT (it, it, it2data);
  7501             goto replaced;
  7502           }
  7503 
  7504         /* Newline is not replaced by anything -- so we are done.  */
  7505         RESTORE_IT (it, it, it2data);
  7506         break;
  7507 
  7508       replaced:
  7509         if (beg < BEGV)
  7510           beg = BEGV;
  7511         IT_CHARPOS (*it) = beg;
  7512         IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
  7513       }
  7514     }
  7515 
  7516   it->continuation_lines_width = 0;
  7517 
  7518   eassert (IT_CHARPOS (*it) >= BEGV);
  7519   eassert (it->medium_narrowing_begv > 0 /* long-line optimizations: all bets off */
  7520            || IT_CHARPOS (*it) == BEGV
  7521            || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7522   CHECK_IT (it);
  7523 }
  7524 
  7525 
  7526 /* Reseat iterator IT at the previous visible line start.  Skip
  7527    invisible text that is so either due to text properties or due to
  7528    selective display.  At the end, update IT's overlay information,
  7529    face information etc.  */
  7530 
  7531 void
  7532 reseat_at_previous_visible_line_start (struct it *it)
  7533 {
  7534   back_to_previous_visible_line_start (it);
  7535   reseat (it, it->current.pos, true);
  7536   CHECK_IT (it);
  7537 }
  7538 
  7539 
  7540 /* Reseat iterator IT on the next visible line start in the current
  7541    buffer.  ON_NEWLINE_P means position IT on the newline
  7542    preceding the line start.  Skip over invisible text that is so
  7543    because of selective display.  Compute faces, overlays etc at the
  7544    new position.  Note that this function does not skip over text that
  7545    is invisible because of text properties.  */
  7546 
  7547 static void
  7548 reseat_at_next_visible_line_start (struct it *it, bool on_newline_p)
  7549 {
  7550   bool skipped_p = false;
  7551   struct bidi_it bidi_it_prev;
  7552   bool newline_found_p
  7553     = forward_to_next_line_start (it, &skipped_p,
  7554                                   on_newline_p ? &bidi_it_prev : NULL);
  7555 
  7556   /* Skip over lines that are invisible because they are indented
  7557      more than the value of IT->selective.  */
  7558   if (it->selective > 0)
  7559     while (IT_CHARPOS (*it) < ZV
  7560            && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
  7561                                  it->selective))
  7562       {
  7563         eassert (IT_BYTEPOS (*it) == BEGV
  7564                  || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
  7565         newline_found_p =
  7566           forward_to_next_line_start (it, &skipped_p,
  7567                                       on_newline_p ? &bidi_it_prev : NULL);
  7568       }
  7569 
  7570   /* Position on the newline if that's what's requested.  */
  7571   if (on_newline_p && newline_found_p)
  7572     {
  7573       if (STRINGP (it->string))
  7574         {
  7575           if (IT_STRING_CHARPOS (*it) > 0)
  7576             {
  7577               if (!it->bidi_p)
  7578                 {
  7579                   --IT_STRING_CHARPOS (*it);
  7580                   --IT_STRING_BYTEPOS (*it);
  7581                 }
  7582               else
  7583                 {
  7584                   /* We need to restore the bidi iterator to the state
  7585                      it had on the newline, and resync the IT's
  7586                      position with that.  */
  7587                   it->bidi_it = bidi_it_prev;
  7588                   IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  7589                   IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  7590                 }
  7591             }
  7592         }
  7593       else if (IT_CHARPOS (*it) > BEGV)
  7594         {
  7595           if (!it->bidi_p)
  7596             {
  7597               --IT_CHARPOS (*it);
  7598               --IT_BYTEPOS (*it);
  7599             }
  7600           else
  7601             {
  7602               /* We need to restore the bidi iterator to the state it
  7603                  had on the newline and resync IT with that.  */
  7604               it->bidi_it = bidi_it_prev;
  7605               IT_CHARPOS (*it) = it->bidi_it.charpos;
  7606               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  7607             }
  7608           reseat (it, it->current.pos, false);
  7609         }
  7610     }
  7611   else if (skipped_p)
  7612     reseat (it, it->current.pos, false);
  7613 
  7614   CHECK_IT (it);
  7615 }
  7616 
  7617 
  7618 
  7619 /***********************************************************************
  7620                    Changing an iterator's position
  7621 ***********************************************************************/
  7622 
  7623 /* Change IT's current position to POS in current_buffer.
  7624    If FORCE_P, always check for text properties at the new position.
  7625    Otherwise, text properties are only looked up if POS >=
  7626    IT->check_charpos of a property.  */
  7627 
  7628 static void
  7629 reseat (struct it *it, struct text_pos pos, bool force_p)
  7630 {
  7631   ptrdiff_t original_pos = IT_CHARPOS (*it);
  7632 
  7633   reseat_1 (it, pos, false);
  7634 
  7635   if (current_buffer->long_line_optimizations_p)
  7636     {
  7637       if (!it->medium_narrowing_begv)
  7638         {
  7639           it->medium_narrowing_begv
  7640             = get_medium_narrowing_begv (it->w, window_point (it->w));
  7641           it->medium_narrowing_zv
  7642             = get_medium_narrowing_zv (it->w, window_point (it->w));
  7643           it->large_narrowing_begv
  7644             = get_large_narrowing_begv (window_point (it->w));
  7645           it->large_narrowing_zv
  7646             = get_large_narrowing_zv (window_point (it->w));
  7647         }
  7648       else if ((pos.charpos < it->medium_narrowing_begv
  7649                 || pos.charpos > it->medium_narrowing_zv)
  7650                 && (!redisplaying_p || it->line_wrap == TRUNCATE))
  7651         {
  7652           it->medium_narrowing_begv
  7653             = get_medium_narrowing_begv (it->w, pos.charpos);
  7654           it->medium_narrowing_zv
  7655             = get_medium_narrowing_zv (it->w, pos.charpos);
  7656           it->large_narrowing_begv
  7657             = get_large_narrowing_begv (window_point (it->w));
  7658           it->large_narrowing_zv
  7659             = get_large_narrowing_zv (window_point (it->w));
  7660         }
  7661     }
  7662 
  7663   /* Determine where to check text properties.  Avoid doing it
  7664      where possible because text property lookup is very expensive.  */
  7665   if (force_p
  7666       || CHARPOS (pos) > it->stop_charpos
  7667       || CHARPOS (pos) < original_pos)
  7668     {
  7669       if (it->bidi_p)
  7670         {
  7671           /* For bidi iteration, we need to prime prev_stop and
  7672              base_level_stop with our best estimations.  */
  7673           /* Implementation note: Of course, POS is not necessarily a
  7674              stop position, so assigning prev_pos to it is a lie; we
  7675              should have called compute_stop_backwards.  However, if
  7676              the current buffer does not include any R2L characters,
  7677              that call would be a waste of cycles, because the
  7678              iterator will never move back, and thus never cross this
  7679              "fake" stop position.  So we delay that backward search
  7680              until the time we really need it, in next_element_from_buffer.  */
  7681           if (CHARPOS (pos) != it->prev_stop)
  7682             it->prev_stop = CHARPOS (pos);
  7683           if (CHARPOS (pos) < it->base_level_stop)
  7684             it->base_level_stop = 0; /* meaning it's unknown */
  7685           handle_stop (it);
  7686         }
  7687       else
  7688         {
  7689           handle_stop (it);
  7690           it->prev_stop = it->base_level_stop = 0;
  7691         }
  7692 
  7693     }
  7694 
  7695   CHECK_IT (it);
  7696 }
  7697 
  7698 
  7699 /* Change IT's buffer position to POS.  SET_STOP_P means set
  7700    IT->stop_pos to POS, also.  */
  7701 
  7702 static void
  7703 reseat_1 (struct it *it, struct text_pos pos, bool set_stop_p)
  7704 {
  7705   /* Don't call this function when scanning a C string.  */
  7706   eassert (it->s == NULL);
  7707 
  7708   /* POS must be a reasonable value.  */
  7709   eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
  7710 
  7711   it->current.pos = it->position = pos;
  7712   it->end_charpos = ZV;
  7713   it->dpvec = NULL;
  7714   it->current.dpvec_index = -1;
  7715   it->current.overlay_string_index = -1;
  7716   IT_STRING_CHARPOS (*it) = -1;
  7717   IT_STRING_BYTEPOS (*it) = -1;
  7718   it->string = Qnil;
  7719   it->method = GET_FROM_BUFFER;
  7720   it->object = it->w->contents;
  7721   it->area = TEXT_AREA;
  7722   it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
  7723   it->sp = 0;
  7724   it->string_from_display_prop_p = false;
  7725   it->string_from_prefix_prop_p = false;
  7726 
  7727   it->from_disp_prop_p = false;
  7728   it->face_before_selective_p = false;
  7729   if (it->bidi_p)
  7730     {
  7731       bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7732                     &it->bidi_it);
  7733       bidi_unshelve_cache (NULL, false);
  7734       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7735       it->bidi_it.string.s = NULL;
  7736       it->bidi_it.string.lstring = Qnil;
  7737       it->bidi_it.string.bufpos = 0;
  7738       it->bidi_it.string.from_disp_str = false;
  7739       it->bidi_it.string.unibyte = false;
  7740       it->bidi_it.w = it->w;
  7741     }
  7742 
  7743   if (set_stop_p)
  7744     {
  7745       it->stop_charpos = CHARPOS (pos);
  7746       it->base_level_stop = CHARPOS (pos);
  7747     }
  7748   /* This make the information stored in it->cmp_it invalidate.  */
  7749   it->cmp_it.id = -1;
  7750   it->min_width_property = Qnil;
  7751 }
  7752 
  7753 
  7754 /* Set up IT for displaying a string, starting at CHARPOS in window W.
  7755    If S is non-null, it is a C string to iterate over.  Otherwise,
  7756    STRING gives a Lisp string to iterate over.
  7757 
  7758    If PRECISION > 0, don't return more then PRECISION number of
  7759    characters from the string.
  7760 
  7761    If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
  7762    characters have been returned.  FIELD_WIDTH < 0 means an infinite
  7763    field width.
  7764 
  7765    MULTIBYTE = 0 means disable processing of multibyte characters,
  7766    MULTIBYTE > 0 means enable it,
  7767    MULTIBYTE < 0 means use IT->multibyte_p.
  7768 
  7769    IT must be initialized via a prior call to init_iterator before
  7770    calling this function.  */
  7771 
  7772 static void
  7773 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
  7774                   ptrdiff_t charpos, ptrdiff_t precision, int field_width,
  7775                   int multibyte)
  7776 {
  7777   /* No text property checks performed by default, but see below.  */
  7778   it->stop_charpos = -1;
  7779 
  7780   /* Set iterator position and end position.  */
  7781   memset (&it->current, 0, sizeof it->current);
  7782   it->current.overlay_string_index = -1;
  7783   it->current.dpvec_index = -1;
  7784   eassert (charpos >= 0);
  7785 
  7786   /* If STRING is specified, use its multibyteness, otherwise use the
  7787      setting of MULTIBYTE, if specified.  */
  7788   if (multibyte >= 0)
  7789     it->multibyte_p = multibyte > 0;
  7790 
  7791   /* Bidirectional reordering of strings is controlled by the default
  7792      value of bidi-display-reordering.  Don't try to reorder while
  7793      loading loadup.el, as the necessary character property tables are
  7794      not yet available.  */
  7795   it->bidi_p =
  7796     !redisplay__inhibit_bidi
  7797     && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
  7798 
  7799   if (s == NULL)
  7800     {
  7801       eassert (STRINGP (string));
  7802       it->string = string;
  7803       it->s = NULL;
  7804       it->end_charpos = it->string_nchars = SCHARS (string);
  7805       it->method = GET_FROM_STRING;
  7806       it->current.string_pos = string_pos (charpos, string);
  7807 
  7808       if (it->bidi_p)
  7809         {
  7810           it->bidi_it.string.lstring = string;
  7811           it->bidi_it.string.s = NULL;
  7812           it->bidi_it.string.schars = it->end_charpos;
  7813           it->bidi_it.string.bufpos = 0;
  7814           it->bidi_it.string.from_disp_str = false;
  7815           it->bidi_it.string.unibyte = !it->multibyte_p;
  7816           it->bidi_it.w = it->w;
  7817           bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
  7818                         FRAME_WINDOW_P (it->f), &it->bidi_it);
  7819         }
  7820     }
  7821   else
  7822     {
  7823       it->s = (const unsigned char *) s;
  7824       it->string = Qnil;
  7825 
  7826       /* Note that we use IT->current.pos, not it->current.string_pos,
  7827          for displaying C strings.  */
  7828       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
  7829       if (it->multibyte_p)
  7830         {
  7831           it->current.pos = c_string_pos (charpos, s, true);
  7832           it->end_charpos = it->string_nchars = number_of_chars (s, true);
  7833         }
  7834       else
  7835         {
  7836           IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
  7837           it->end_charpos = it->string_nchars = strlen (s);
  7838         }
  7839 
  7840       if (it->bidi_p)
  7841         {
  7842           it->bidi_it.string.lstring = Qnil;
  7843           it->bidi_it.string.s = (const unsigned char *) s;
  7844           it->bidi_it.string.schars = it->end_charpos;
  7845           it->bidi_it.string.bufpos = 0;
  7846           it->bidi_it.string.from_disp_str = false;
  7847           it->bidi_it.string.unibyte = !it->multibyte_p;
  7848           it->bidi_it.w = it->w;
  7849           bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
  7850                         &it->bidi_it);
  7851         }
  7852       it->method = GET_FROM_C_STRING;
  7853     }
  7854 
  7855   /* PRECISION > 0 means don't return more than PRECISION characters
  7856      from the string.  */
  7857   if (precision > 0 && it->end_charpos - charpos > precision)
  7858     {
  7859       it->end_charpos = it->string_nchars = charpos + precision;
  7860       if (it->bidi_p)
  7861         it->bidi_it.string.schars = it->end_charpos;
  7862     }
  7863 
  7864   /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
  7865      characters have been returned.  FIELD_WIDTH == 0 means don't pad,
  7866      FIELD_WIDTH < 0 means infinite field width.  This is useful for
  7867      padding with `-' at the end of a mode line.  */
  7868   if (field_width < 0)
  7869     field_width = DISP_INFINITY;
  7870   /* Implementation note: We deliberately don't enlarge
  7871      it->bidi_it.string.schars here to fit it->end_charpos, because
  7872      the bidi iterator cannot produce characters out of thin air.  */
  7873   if (field_width > it->end_charpos - charpos)
  7874     it->end_charpos = charpos + field_width;
  7875 
  7876   /* Use the standard display table for displaying strings.  */
  7877   if (DISP_TABLE_P (Vstandard_display_table))
  7878     it->dp = XCHAR_TABLE (Vstandard_display_table);
  7879 
  7880   it->stop_charpos = charpos;
  7881   it->prev_stop = charpos;
  7882   it->base_level_stop = 0;
  7883   if (it->bidi_p)
  7884     {
  7885       it->bidi_it.first_elt = true;
  7886       it->bidi_it.paragraph_dir = NEUTRAL_DIR;
  7887       it->bidi_it.disp_pos = -1;
  7888     }
  7889   if (s == NULL && it->multibyte_p)
  7890     {
  7891       ptrdiff_t endpos = SCHARS (it->string);
  7892       if (endpos > it->end_charpos)
  7893         endpos = it->end_charpos;
  7894       composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
  7895                                     it->string, true);
  7896     }
  7897   CHECK_IT (it);
  7898 }
  7899 
  7900 
  7901 
  7902 /***********************************************************************
  7903                               Iteration
  7904 ***********************************************************************/
  7905 
  7906 /* Map enum it_method value to corresponding next_element_from_* function.  */
  7907 
  7908 typedef bool (*next_element_function) (struct it *);
  7909 
  7910 static next_element_function const get_next_element[NUM_IT_METHODS] =
  7911 {
  7912   next_element_from_buffer,
  7913   next_element_from_display_vector,
  7914   next_element_from_string,
  7915   next_element_from_c_string,
  7916   next_element_from_image,
  7917   next_element_from_stretch,
  7918   next_element_from_xwidget,
  7919 };
  7920 
  7921 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
  7922 
  7923 
  7924 /* Return true iff a character at CHARPOS (and BYTEPOS) is composed
  7925    (possibly with the following characters).
  7926 
  7927   Note: we pass -1 as the "resolved bidi level" when the iterator
  7928   doesn't have the bidi_p flag set, because in that case we really
  7929   don't know what is the directionality of the text, so we leave it to
  7930   the shaping engine to figure that out.  */
  7931 
  7932 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS)                 \
  7933   ((IT)->cmp_it.id >= 0                                                 \
  7934    || ((IT)->cmp_it.stop_pos == (CHARPOS)                               \
  7935        && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS,       \
  7936                                  END_CHARPOS, (IT)->w,                  \
  7937                                  (IT)->bidi_p                           \
  7938                                  ? (IT)->bidi_it.resolved_level         \
  7939                                  : -1,                                  \
  7940                                  FACE_FROM_ID_OR_NULL ((IT)->f,         \
  7941                                                        (IT)->face_id),  \
  7942                                  (IT)->string)))
  7943 
  7944 
  7945 /* Lookup the char-table Vglyphless_char_display for character C (-1
  7946    if we want information for no-font case), and return the display
  7947    method symbol.  By side-effect, update it->what and
  7948    it->glyphless_method.  This function is called from
  7949    get_next_display_element for each character element, and from
  7950    gui_produce_glyphs when no suitable font was found.  */
  7951 
  7952 Lisp_Object
  7953 lookup_glyphless_char_display (int c, struct it *it)
  7954 {
  7955   Lisp_Object glyphless_method = Qnil;
  7956 
  7957   if (CHAR_TABLE_P (Vglyphless_char_display)
  7958       && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
  7959     {
  7960       if (c >= 0)
  7961         glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
  7962       else
  7963         glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
  7964 
  7965       if (CONSP (glyphless_method))
  7966         glyphless_method = FRAME_WINDOW_P (it->f)
  7967           ? XCAR (glyphless_method)
  7968           : XCDR (glyphless_method);
  7969     }
  7970 
  7971  retry:
  7972   if (NILP (glyphless_method))
  7973     {
  7974       if (c >= 0)
  7975         /* The default is to display the character by a proper font.  */
  7976         return Qnil;
  7977       /* The default for the no-font case is to display an empty box.  */
  7978       glyphless_method = Qempty_box;
  7979     }
  7980   if (EQ (glyphless_method, Qzero_width))
  7981     {
  7982       if (c >= 0)
  7983         return glyphless_method;
  7984       /* This method can't be used for the no-font case.  */
  7985       glyphless_method = Qempty_box;
  7986     }
  7987   if (EQ (glyphless_method, Qthin_space))
  7988     it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
  7989   else if (EQ (glyphless_method, Qempty_box))
  7990     it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
  7991   else if (EQ (glyphless_method, Qhex_code))
  7992     it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
  7993   else if (STRINGP (glyphless_method))
  7994     it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
  7995   else
  7996     {
  7997       /* Invalid value.  We use the default method.  */
  7998       glyphless_method = Qnil;
  7999       goto retry;
  8000     }
  8001   it->what = IT_GLYPHLESS;
  8002   return glyphless_method;
  8003 }
  8004 
  8005 /* Merge escape glyph face and cache the result.  */
  8006 
  8007 static struct frame *last_escape_glyph_frame = NULL;
  8008 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8009 static int last_escape_glyph_merged_face_id = 0;
  8010 
  8011 static int
  8012 merge_escape_glyph_face (struct it *it)
  8013 {
  8014   int face_id;
  8015 
  8016   if (it->f == last_escape_glyph_frame
  8017       && it->face_id == last_escape_glyph_face_id)
  8018     face_id = last_escape_glyph_merged_face_id;
  8019   else
  8020     {
  8021       /* Merge the `escape-glyph' face into the current face.  */
  8022       face_id = merge_faces (it->w, Qescape_glyph, 0, it->face_id);
  8023       last_escape_glyph_frame = it->f;
  8024       last_escape_glyph_face_id = it->face_id;
  8025       last_escape_glyph_merged_face_id = face_id;
  8026     }
  8027   return face_id;
  8028 }
  8029 
  8030 /* Likewise for glyphless glyph face.  */
  8031 
  8032 static struct frame *last_glyphless_glyph_frame = NULL;
  8033 static int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8034 static int last_glyphless_glyph_merged_face_id = 0;
  8035 
  8036 int
  8037 merge_glyphless_glyph_face (struct it *it)
  8038 {
  8039   int face_id;
  8040 
  8041   if (it->f == last_glyphless_glyph_frame
  8042       && it->face_id == last_glyphless_glyph_face_id)
  8043     face_id = last_glyphless_glyph_merged_face_id;
  8044   else
  8045     {
  8046       /* Merge the `glyphless-char' face into the current face.  */
  8047       face_id = merge_faces (it->w, Qglyphless_char, 0, it->face_id);
  8048       last_glyphless_glyph_frame = it->f;
  8049       last_glyphless_glyph_face_id = it->face_id;
  8050       last_glyphless_glyph_merged_face_id = face_id;
  8051     }
  8052   return face_id;
  8053 }
  8054 
  8055 /* Forget the `escape-glyph' and `glyphless-char' faces.  This should
  8056    be called before redisplaying windows, and when the frame's face
  8057    cache is freed.  */
  8058 void
  8059 forget_escape_and_glyphless_faces (void)
  8060 {
  8061   last_escape_glyph_frame = NULL;
  8062   last_escape_glyph_face_id = (1 << FACE_ID_BITS);
  8063   last_glyphless_glyph_frame = NULL;
  8064   last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
  8065 }
  8066 
  8067 /* Load IT's display element fields with information about the next
  8068    display element from the current position of IT.  Value is false if
  8069    end of buffer (or C string) is reached.  */
  8070 
  8071 static bool
  8072 get_next_display_element (struct it *it)
  8073 {
  8074   /* True means that we found a display element.  False means that
  8075      we hit the end of what we iterate over.  Performance note: the
  8076      function pointer `method' used here turns out to be faster than
  8077      using a sequence of if-statements.  */
  8078   bool success_p;
  8079 
  8080  get_next:
  8081   success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  8082 
  8083   if (it->what == IT_CHARACTER)
  8084     {
  8085       /* UAX#9, L4: "A character is depicted by a mirrored glyph if
  8086          and only if (a) the resolved directionality of that character
  8087          is R..."  */
  8088       /* FIXME: Do we need an exception for characters from display
  8089          tables?  */
  8090       if (it->bidi_p && it->bidi_it.type == STRONG_R
  8091           && !inhibit_bidi_mirroring)
  8092         it->c = bidi_mirror_char (it->c);
  8093       /* Map via display table or translate control characters.
  8094          IT->c, IT->len etc. have been set to the next character by
  8095          the function call above.  If we have a display table, and it
  8096          contains an entry for IT->c, translate it.  Don't do this if
  8097          IT->c itself comes from a display table, otherwise we could
  8098          end up in an infinite recursion.  (An alternative could be to
  8099          count the recursion depth of this function and signal an
  8100          error when a certain maximum depth is reached.)  Is it worth
  8101          it?  */
  8102       if (success_p && it->dpvec == NULL)
  8103         {
  8104           Lisp_Object dv;
  8105           struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
  8106           bool nonascii_space_p = false;
  8107           bool nonascii_hyphen_p = false;
  8108           int c = it->c;        /* This is the character to display.  */
  8109 
  8110           if (! it->multibyte_p && ! ASCII_CHAR_P (c))
  8111             {
  8112               eassert (SINGLE_BYTE_CHAR_P (c));
  8113               if (unibyte_display_via_language_environment)
  8114                 {
  8115                   c = DECODE_CHAR (unibyte, c);
  8116                   if (c < 0)
  8117                     c = BYTE8_TO_CHAR (it->c);
  8118                 }
  8119               else
  8120                 c = BYTE8_TO_CHAR (it->c);
  8121             }
  8122 
  8123           if (it->dp
  8124               && (dv = DISP_CHAR_VECTOR (it->dp, c),
  8125                   VECTORP (dv)))
  8126             {
  8127               struct Lisp_Vector *v = XVECTOR (dv);
  8128 
  8129               /* Return the first character from the display table
  8130                  entry, if not empty.  If empty, don't display the
  8131                  current character.  */
  8132               if (v->header.size)
  8133                 {
  8134                   it->dpvec_char_len = it->len;
  8135                   it->dpvec = v->contents;
  8136                   it->dpend = v->contents + v->header.size;
  8137                   it->current.dpvec_index = 0;
  8138                   it->dpvec_face_id = -1;
  8139                   it->saved_face_id = it->face_id;
  8140                   it->method = GET_FROM_DISPLAY_VECTOR;
  8141                   it->ellipsis_p = false;
  8142                 }
  8143               else
  8144                 {
  8145                   set_iterator_to_next (it, false);
  8146                 }
  8147               goto get_next;
  8148             }
  8149 
  8150           if (! NILP (lookup_glyphless_char_display (c, it)))
  8151             {
  8152               if (it->what == IT_GLYPHLESS)
  8153                 goto done;
  8154               /* Don't display this character.  */
  8155               set_iterator_to_next (it, false);
  8156               goto get_next;
  8157             }
  8158 
  8159           /* If `nobreak-char-display' is non-nil, we display
  8160              non-ASCII spaces and hyphens specially.  */
  8161           if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
  8162             {
  8163               if (blankp (c))
  8164                 nonascii_space_p = true;
  8165               else if (c == SOFT_HYPHEN || c == HYPHEN
  8166                        || c == NON_BREAKING_HYPHEN)
  8167                 nonascii_hyphen_p = true;
  8168             }
  8169 
  8170           /* Translate control characters into `\003' or `^C' form.
  8171              Control characters coming from a display table entry are
  8172              currently not translated because we use IT->dpvec to hold
  8173              the translation.  This could easily be changed but I
  8174              don't believe that it is worth doing.
  8175 
  8176              The characters handled by `nobreak-char-display' must be
  8177              translated too.
  8178 
  8179              Non-printable characters and raw-byte characters are also
  8180              translated to octal or hexadecimal form.  */
  8181           if (((c < ' ' || c == 127) /* ASCII control chars.  */
  8182                ? (it->area != TEXT_AREA
  8183                   /* In mode line, treat \n, \t like other crl chars.  */
  8184                   || (c != '\t'
  8185                       && it->glyph_row
  8186                       && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
  8187                   || (c != '\n' && c != '\t'))
  8188                : (nonascii_space_p
  8189                   || nonascii_hyphen_p
  8190                   || CHAR_BYTE8_P (c)
  8191                   || ! CHAR_PRINTABLE_P (c))))
  8192             {
  8193               /* C is a control character, non-ASCII space/hyphen,
  8194                  raw-byte, or a non-printable character which must be
  8195                  displayed either as '\003' or as `^C' where the '\\'
  8196                  and '^' can be defined in the display table.  Fill
  8197                  IT->ctl_chars with glyphs for what we have to
  8198                  display.  Then, set IT->dpvec to these glyphs.  */
  8199               Lisp_Object gc;
  8200               int ctl_len;
  8201               int face_id;
  8202               int lface_id = 0;
  8203               int escape_glyph;
  8204 
  8205               /* Handle control characters with ^.  */
  8206 
  8207               if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
  8208                 {
  8209                   int g;
  8210 
  8211                   g = '^';           /* default glyph for Control */
  8212                   /* Set IT->ctl_chars[0] to the glyph for `^'.  */
  8213                   if (it->dp
  8214                       && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8215                     {
  8216                       g = GLYPH_CODE_CHAR (gc);
  8217                       lface_id = GLYPH_CODE_FACE (gc);
  8218                     }
  8219 
  8220                   face_id = (lface_id
  8221                              ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8222                              : merge_escape_glyph_face (it));
  8223 
  8224                   XSETINT (it->ctl_chars[0], g);
  8225                   XSETINT (it->ctl_chars[1], c ^ 0100);
  8226                   ctl_len = 2;
  8227                   goto display_control;
  8228                 }
  8229 
  8230               /* Handle non-ascii space in the mode where it only gets
  8231                  highlighting.  */
  8232 
  8233               if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
  8234                 {
  8235                   /* Merge `nobreak-space' into the current face.  */
  8236                   face_id = merge_faces (it->w, Qnobreak_space, 0,
  8237                                          it->face_id);
  8238                   XSETINT (it->ctl_chars[0],
  8239                            nobreak_char_ascii_display ? ' ' : it->c);
  8240                   ctl_len = 1;
  8241                   goto display_control;
  8242                 }
  8243 
  8244               /* Handle non-ascii hyphens in the mode where it only
  8245                  gets highlighting.  */
  8246 
  8247               if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
  8248                 {
  8249                   /* Merge `nobreak-space' into the current face.  */
  8250                   face_id = merge_faces (it->w, Qnobreak_hyphen, 0,
  8251                                          it->face_id);
  8252                   XSETINT (it->ctl_chars[0],
  8253                            nobreak_char_ascii_display ? '-' : it->c);
  8254                   ctl_len = 1;
  8255                   goto display_control;
  8256                 }
  8257 
  8258               /* Handle sequences that start with the "escape glyph".  */
  8259 
  8260               /* the default escape glyph is \.  */
  8261               escape_glyph = '\\';
  8262 
  8263               if (it->dp
  8264                   && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
  8265                 {
  8266                   escape_glyph = GLYPH_CODE_CHAR (gc);
  8267                   lface_id = GLYPH_CODE_FACE (gc);
  8268                 }
  8269 
  8270               face_id = (lface_id
  8271                          ? merge_faces (it->w, Qt, lface_id, it->face_id)
  8272                          : merge_escape_glyph_face (it));
  8273 
  8274               /* Draw non-ASCII space/hyphen with escape glyph: */
  8275 
  8276               if (nonascii_space_p || nonascii_hyphen_p)
  8277                 {
  8278                   XSETINT (it->ctl_chars[0], escape_glyph);
  8279                   XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
  8280                   ctl_len = 2;
  8281                   goto display_control;
  8282                 }
  8283 
  8284               {
  8285                 char str[10];
  8286                 int len, i;
  8287 
  8288                 if (CHAR_BYTE8_P (c))
  8289                   /* Display \200 or \x80 instead of \17777600.  */
  8290                   c = CHAR_TO_BYTE8 (c);
  8291                 const char *format_string = display_raw_bytes_as_hex
  8292                                             ? "x%02x"
  8293                                             : "%03o";
  8294                 len = sprintf (str, format_string, c + 0u);
  8295 
  8296                 XSETINT (it->ctl_chars[0], escape_glyph);
  8297                 for (i = 0; i < len; i++)
  8298                   XSETINT (it->ctl_chars[i + 1], str[i]);
  8299                 ctl_len = len + 1;
  8300               }
  8301 
  8302             display_control:
  8303               /* Set up IT->dpvec and return first character from it.  */
  8304               it->dpvec_char_len = it->len;
  8305               it->dpvec = it->ctl_chars;
  8306               it->dpend = it->dpvec + ctl_len;
  8307               it->current.dpvec_index = 0;
  8308               it->dpvec_face_id = face_id;
  8309               it->saved_face_id = it->face_id;
  8310               it->method = GET_FROM_DISPLAY_VECTOR;
  8311               it->ellipsis_p = false;
  8312               goto get_next;
  8313             }
  8314           it->char_to_display = c;
  8315         }
  8316       else if (success_p)
  8317         {
  8318           it->char_to_display = it->c;
  8319         }
  8320     }
  8321 
  8322 #ifdef HAVE_WINDOW_SYSTEM
  8323   /* Adjust face id for a multibyte character.  There are no multibyte
  8324      character in unibyte text.  */
  8325   if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
  8326       && it->multibyte_p
  8327       && success_p
  8328       && FRAME_WINDOW_P (it->f))
  8329     {
  8330       struct face *face = FACE_FROM_ID (it->f, it->face_id);
  8331 
  8332       if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
  8333         {
  8334           /* Automatic composition with glyph-string.   */
  8335           Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
  8336 
  8337           it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
  8338         }
  8339       else
  8340         {
  8341           ptrdiff_t pos = (it->s ? -1
  8342                      : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
  8343                      : IT_CHARPOS (*it));
  8344           int c;
  8345 
  8346           if (it->what == IT_CHARACTER)
  8347             c = it->char_to_display;
  8348           else
  8349             {
  8350               struct composition *cmp = composition_table[it->cmp_it.id];
  8351               int i;
  8352 
  8353               c = ' ';
  8354               for (i = 0; i < cmp->glyph_len; i++)
  8355                 /* TAB in a composition means display glyphs with
  8356                    padding space on the left or right.  */
  8357                 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
  8358                   break;
  8359             }
  8360           it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
  8361         }
  8362     }
  8363 #endif  /* HAVE_WINDOW_SYSTEM */
  8364 
  8365  done:
  8366   /* Is this character the last one of a run of characters with
  8367      box?  If yes, set IT->end_of_box_run_p to true.  */
  8368   if (it->face_box_p
  8369       && it->s == NULL)
  8370     {
  8371       if (it->method == GET_FROM_STRING && it->sp)
  8372         {
  8373           int face_id = underlying_face_id (it);
  8374           struct face *face = FACE_FROM_ID_OR_NULL (it->f, face_id);
  8375 
  8376           if (face)
  8377             {
  8378               if (face->box == FACE_NO_BOX)
  8379                 {
  8380                   /* If the box comes from face properties in a
  8381                      display string, check faces in that string.  */
  8382                   int string_face_id = face_after_it_pos (it);
  8383                   if (FACE_FROM_ID (it->f, string_face_id)->box == FACE_NO_BOX)
  8384                     it->end_of_box_run_p = true;
  8385                 }
  8386               /* Otherwise, the box comes from the underlying face.
  8387                  If this is the last string character displayed, check
  8388                  the next buffer location.  */
  8389               else if (((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
  8390                         /* For a composition, see if the string ends
  8391                            at the last character included in the
  8392                            composition.  */
  8393                         || (it->what == IT_COMPOSITION
  8394                             && (IT_STRING_CHARPOS (*it) + it->cmp_it.nchars
  8395                                 >= SCHARS (it->string))))
  8396                        /* n_overlay_strings is unreliable unless
  8397                           overlay_string_index is non-negative.  */
  8398                        && ((it->current.overlay_string_index >= 0
  8399                             && (it->current.overlay_string_index
  8400                                 == it->n_overlay_strings - 1))
  8401                            /* A string from display property.  */
  8402                            || it->from_disp_prop_p))
  8403                 {
  8404                   ptrdiff_t ignore;
  8405                   int next_face_id;
  8406                   bool text_from_string = false;
  8407                   /* Normally, the next buffer location is stored in
  8408                      IT->current.pos...  */
  8409                   struct text_pos pos = it->current.pos;
  8410 
  8411                   /* ...but for a string from a display property, the
  8412                      next buffer position is stored in the 'position'
  8413                      member of the iteration stack slot below the
  8414                      current one, see handle_single_display_spec.  By
  8415                      contrast, it->current.pos was not yet updated to
  8416                      point to that buffer position; that will happen
  8417                      in pop_it, after we finish displaying the current
  8418                      string.  Note that we already checked above that
  8419                      it->sp is positive, so subtracting one from it is
  8420                      safe.  */
  8421                   if (it->from_disp_prop_p)
  8422                     {
  8423                       int stackp = it->sp - 1;
  8424 
  8425                       /* Find the stack level with data from buffer.  */
  8426                       while (stackp >= 0
  8427                              && STRINGP ((it->stack + stackp)->string))
  8428                         stackp--;
  8429                       if (stackp < 0)
  8430                         {
  8431                           /* If no stack slot was found for iterating
  8432                              a buffer, we are displaying text from a
  8433                              string, most probably the mode line or
  8434                              the header line, and that string has a
  8435                              display string on some of its
  8436                              characters.  */
  8437                           text_from_string = true;
  8438                           pos = it->stack[it->sp - 1].position;
  8439                         }
  8440                       else
  8441                         pos = (it->stack + stackp)->position;
  8442                     }
  8443                   else
  8444                     INC_TEXT_POS (pos, it->multibyte_p);
  8445 
  8446                   if (text_from_string)
  8447                     {
  8448                       Lisp_Object base_string = it->stack[it->sp - 1].string;
  8449 
  8450                       if (CHARPOS (pos) >= SCHARS (base_string) - 1)
  8451                         it->end_of_box_run_p = true;
  8452                       else
  8453                         {
  8454                           next_face_id
  8455                             = face_at_string_position (it->w, base_string,
  8456                                                        CHARPOS (pos), 0,
  8457                                                        &ignore, face_id,
  8458                                                        false, 0);
  8459                           if (FACE_FROM_ID (it->f, next_face_id)->box
  8460                               == FACE_NO_BOX)
  8461                             it->end_of_box_run_p = true;
  8462                         }
  8463                     }
  8464                   else if (CHARPOS (pos) >= ZV)
  8465                     it->end_of_box_run_p = true;
  8466                   else
  8467                     {
  8468                       next_face_id =
  8469                         face_at_buffer_position (it->w, CHARPOS (pos),
  8470                                                  &ignore,
  8471                                                  CHARPOS (pos)
  8472                                                  + TEXT_PROP_DISTANCE_LIMIT,
  8473                                                  false, -1, 0);
  8474                       if (FACE_FROM_ID (it->f, next_face_id)->box
  8475                           == FACE_NO_BOX)
  8476                         it->end_of_box_run_p = true;
  8477                     }
  8478                 }
  8479             }
  8480         }
  8481       /* next_element_from_display_vector sets this flag according to
  8482          faces of the display vector glyphs, see there.  */
  8483       else if (it->method != GET_FROM_DISPLAY_VECTOR)
  8484         {
  8485           int face_id = face_after_it_pos (it);
  8486           if (face_id != it->face_id
  8487               && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX)
  8488             it->end_of_box_run_p = true;
  8489         }
  8490     }
  8491   /* If we reached the end of the object we've been iterating (e.g., a
  8492      display string or an overlay string), and there's something on
  8493      IT->stack, proceed with what's on the stack.  It doesn't make
  8494      sense to return false if there's unprocessed stuff on the stack,
  8495      because otherwise that stuff will never be displayed.  */
  8496   if (!success_p && it->sp > 0)
  8497     {
  8498       set_iterator_to_next (it, false);
  8499       success_p = get_next_display_element (it);
  8500     }
  8501 
  8502   /* Value is false if end of buffer or string reached.  */
  8503   return success_p;
  8504 }
  8505 
  8506 
  8507 /* Move IT to the next display element.
  8508 
  8509    RESEAT_P means if called on a newline in buffer text,
  8510    skip to the next visible line start.
  8511 
  8512    Functions get_next_display_element and set_iterator_to_next are
  8513    separate because I find this arrangement easier to handle than a
  8514    get_next_display_element function that also increments IT's
  8515    position.  The way it is we can first look at an iterator's current
  8516    display element, decide whether it fits on a line, and if it does,
  8517    increment the iterator position.  The other way around we probably
  8518    would either need a flag indicating whether the iterator has to be
  8519    incremented the next time, or we would have to implement a
  8520    decrement position function which would not be easy to write.  */
  8521 
  8522 void
  8523 set_iterator_to_next (struct it *it, bool reseat_p)
  8524 {
  8525 
  8526   if (max_redisplay_ticks > 0)
  8527     update_redisplay_ticks (1, it->w);
  8528 
  8529   switch (it->method)
  8530     {
  8531     case GET_FROM_BUFFER:
  8532       /* The current display element of IT is a character from
  8533          current_buffer.  Advance in the buffer, and maybe skip over
  8534          invisible lines that are so because of selective display.  */
  8535       if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
  8536         reseat_at_next_visible_line_start (it, false);
  8537       else if (it->cmp_it.id >= 0)
  8538         {
  8539           /* We are currently getting glyphs from a composition.  */
  8540           if (! it->bidi_p)
  8541             {
  8542               IT_CHARPOS (*it) += it->cmp_it.nchars;
  8543               IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  8544             }
  8545           else
  8546             {
  8547               int i;
  8548 
  8549               /* Update IT's char/byte positions to point to the first
  8550                  character of the next grapheme cluster, or to the
  8551                  character visually after the current composition.  */
  8552               for (i = 0; i < it->cmp_it.nchars; i++)
  8553                 bidi_move_to_visually_next (&it->bidi_it);
  8554               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8555               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8556             }
  8557 
  8558           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8559               && it->cmp_it.to < it->cmp_it.nglyphs)
  8560             {
  8561               /* Composition created while scanning forward.  Proceed
  8562                  to the next grapheme cluster.  */
  8563               it->cmp_it.from = it->cmp_it.to;
  8564             }
  8565           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8566                    && it->cmp_it.from > 0)
  8567             {
  8568               /* Composition created while scanning backward.  Proceed
  8569                  to the previous grapheme cluster.  */
  8570               it->cmp_it.to = it->cmp_it.from;
  8571             }
  8572           else
  8573             {
  8574               /* No more grapheme clusters in this composition.
  8575                  Find the next stop position.  */
  8576               ptrdiff_t stop = it->end_charpos;
  8577 
  8578               if (it->bidi_it.scan_dir < 0)
  8579                 /* Now we are scanning backward and don't know
  8580                    where to stop.  */
  8581                 stop = -1;
  8582               composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8583                                             IT_BYTEPOS (*it), stop, Qnil, true);
  8584             }
  8585         }
  8586       else
  8587         {
  8588           eassert (it->len != 0);
  8589 
  8590           if (!it->bidi_p)
  8591             {
  8592               IT_BYTEPOS (*it) += it->len;
  8593               IT_CHARPOS (*it) += 1;
  8594             }
  8595           else
  8596             {
  8597               int prev_scan_dir = it->bidi_it.scan_dir;
  8598               /* If this is a new paragraph, determine its base
  8599                  direction (a.k.a. its base embedding level).  */
  8600               if (it->bidi_it.new_paragraph)
  8601                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  8602                                      false);
  8603               bidi_move_to_visually_next (&it->bidi_it);
  8604               IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8605               IT_CHARPOS (*it) = it->bidi_it.charpos;
  8606               if (prev_scan_dir != it->bidi_it.scan_dir)
  8607                 {
  8608                   /* As the scan direction was changed, we must
  8609                      re-compute the stop position for composition.  */
  8610                   ptrdiff_t stop = it->end_charpos;
  8611                   if (it->bidi_it.scan_dir < 0)
  8612                     stop = -1;
  8613                   composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
  8614                                                 IT_BYTEPOS (*it), stop, Qnil,
  8615                                                 true);
  8616                 }
  8617             }
  8618           eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
  8619         }
  8620       break;
  8621 
  8622     case GET_FROM_C_STRING:
  8623       /* Current display element of IT is from a C string.  */
  8624       if (!it->bidi_p
  8625           /* If the string position is beyond string's end, it means
  8626              next_element_from_c_string is padding the string with
  8627              blanks, in which case we bypass the bidi iterator,
  8628              because it cannot deal with such virtual characters.  */
  8629           || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
  8630         {
  8631           IT_BYTEPOS (*it) += it->len;
  8632           IT_CHARPOS (*it) += 1;
  8633         }
  8634       else
  8635         {
  8636           bidi_move_to_visually_next (&it->bidi_it);
  8637           IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  8638           IT_CHARPOS (*it) = it->bidi_it.charpos;
  8639         }
  8640       break;
  8641 
  8642     case GET_FROM_DISPLAY_VECTOR:
  8643       /* Current display element of IT is from a display table entry.
  8644          Advance in the display table definition.  Reset it to null if
  8645          end reached, and continue with characters from buffers/
  8646          strings.  */
  8647       ++it->current.dpvec_index;
  8648 
  8649       /* Restore face of the iterator to what they were before the
  8650          display vector entry (these entries may contain faces).  */
  8651       it->face_id = it->saved_face_id;
  8652 
  8653       if (it->dpvec + it->current.dpvec_index >= it->dpend)
  8654         {
  8655           bool recheck_faces = it->ellipsis_p;
  8656 
  8657           if (it->s)
  8658             it->method = GET_FROM_C_STRING;
  8659           else if (STRINGP (it->string))
  8660             it->method = GET_FROM_STRING;
  8661           else
  8662             {
  8663               it->method = GET_FROM_BUFFER;
  8664               it->object = it->w->contents;
  8665             }
  8666 
  8667           it->dpvec = NULL;
  8668           it->current.dpvec_index = -1;
  8669 
  8670           /* Skip over characters which were displayed via IT->dpvec.  */
  8671           if (it->dpvec_char_len < 0)
  8672             reseat_at_next_visible_line_start (it, true);
  8673           else if (it->dpvec_char_len > 0)
  8674             {
  8675               it->len = it->dpvec_char_len;
  8676               set_iterator_to_next (it, reseat_p);
  8677             }
  8678 
  8679           /* Maybe recheck faces after display vector.  */
  8680           if (recheck_faces)
  8681             {
  8682               if (it->method == GET_FROM_STRING)
  8683                 it->stop_charpos = IT_STRING_CHARPOS (*it);
  8684               else
  8685                 it->stop_charpos = IT_CHARPOS (*it);
  8686             }
  8687         }
  8688       break;
  8689 
  8690     case GET_FROM_STRING:
  8691       /* Current display element is a character from a Lisp string.  */
  8692       eassert (it->s == NULL && STRINGP (it->string));
  8693       /* Don't advance past string end.  These conditions are true
  8694          when set_iterator_to_next is called at the end of
  8695          get_next_display_element, in which case the Lisp string is
  8696          already exhausted, and all we want is pop the iterator
  8697          stack.  */
  8698       if (it->current.overlay_string_index >= 0)
  8699         {
  8700           /* This is an overlay string, so there's no padding with
  8701              spaces, and the number of characters in the string is
  8702              where the string ends.  */
  8703           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8704             goto consider_string_end;
  8705         }
  8706       else
  8707         {
  8708           /* Not an overlay string.  There could be padding, so test
  8709              against it->end_charpos.  */
  8710           if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  8711             goto consider_string_end;
  8712         }
  8713       if (it->cmp_it.id >= 0)
  8714         {
  8715           /* We are delivering display elements from a composition.
  8716              Update the string position past the grapheme cluster
  8717              we've just processed.  */
  8718           if (! it->bidi_p)
  8719             {
  8720               IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  8721               IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  8722             }
  8723           else
  8724             {
  8725               int i;
  8726 
  8727               for (i = 0; i < it->cmp_it.nchars; i++)
  8728                 bidi_move_to_visually_next (&it->bidi_it);
  8729               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8730               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8731             }
  8732 
  8733           /* Did we exhaust all the grapheme clusters of this
  8734              composition?  */
  8735           if ((! it->bidi_p || ! it->cmp_it.reversed_p)
  8736               && (it->cmp_it.to < it->cmp_it.nglyphs))
  8737             {
  8738               /* Not all the grapheme clusters were processed yet;
  8739                  advance to the next cluster.  */
  8740               it->cmp_it.from = it->cmp_it.to;
  8741             }
  8742           else if ((it->bidi_p && it->cmp_it.reversed_p)
  8743                    && it->cmp_it.from > 0)
  8744             {
  8745               /* Likewise: advance to the next cluster, but going in
  8746                  the reverse direction.  */
  8747               it->cmp_it.to = it->cmp_it.from;
  8748             }
  8749           else
  8750             {
  8751               /* This composition was fully processed; find the next
  8752                  candidate place for checking for composed
  8753                  characters.  */
  8754               /* Always limit string searches to the string length;
  8755                  any padding spaces are not part of the string, and
  8756                  there cannot be any compositions in that padding.  */
  8757               ptrdiff_t stop = SCHARS (it->string);
  8758 
  8759               if (it->bidi_p && it->bidi_it.scan_dir < 0)
  8760                 stop = -1;
  8761               else if (it->end_charpos < stop)
  8762                 {
  8763                   /* Cf. PRECISION in reseat_to_string: we might be
  8764                      limited in how many of the string characters we
  8765                      need to deliver.  */
  8766                   stop = it->end_charpos;
  8767                 }
  8768               composition_compute_stop_pos (&it->cmp_it,
  8769                                             IT_STRING_CHARPOS (*it),
  8770                                             IT_STRING_BYTEPOS (*it), stop,
  8771                                             it->string, true);
  8772             }
  8773         }
  8774       else
  8775         {
  8776           if (!it->bidi_p
  8777               /* If the string position is beyond string's end, it
  8778                  means next_element_from_string is padding the string
  8779                  with blanks, in which case we bypass the bidi
  8780                  iterator, because it cannot deal with such virtual
  8781                  characters.  */
  8782               || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
  8783             {
  8784               IT_STRING_BYTEPOS (*it) += it->len;
  8785               IT_STRING_CHARPOS (*it) += 1;
  8786             }
  8787           else
  8788             {
  8789               int prev_scan_dir = it->bidi_it.scan_dir;
  8790 
  8791               bidi_move_to_visually_next (&it->bidi_it);
  8792               IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  8793               IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  8794               /* If the scan direction changes, we may need to update
  8795                  the place where to check for composed characters.  */
  8796               if (prev_scan_dir != it->bidi_it.scan_dir)
  8797                 {
  8798                   ptrdiff_t stop = SCHARS (it->string);
  8799 
  8800                   if (it->bidi_it.scan_dir < 0)
  8801                     stop = -1;
  8802                   else if (it->end_charpos < stop)
  8803                     stop = it->end_charpos;
  8804 
  8805                   composition_compute_stop_pos (&it->cmp_it,
  8806                                                 IT_STRING_CHARPOS (*it),
  8807                                                 IT_STRING_BYTEPOS (*it), stop,
  8808                                                 it->string, true);
  8809                 }
  8810             }
  8811         }
  8812 
  8813     consider_string_end:
  8814 
  8815       if (it->current.overlay_string_index >= 0)
  8816         {
  8817           /* IT->string is an overlay string.  Advance to the
  8818              next, if there is one.  */
  8819           if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  8820             {
  8821               it->ellipsis_p = false;
  8822               next_overlay_string (it);
  8823               if (it->ellipsis_p)
  8824                 setup_for_ellipsis (it, 0);
  8825             }
  8826         }
  8827       else
  8828         {
  8829           /* IT->string is not an overlay string.  If we reached
  8830              its end, and there is something on IT->stack, proceed
  8831              with what is on the stack.  This can be either another
  8832              string, this time an overlay string, or a buffer.  */
  8833           if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
  8834               && it->sp > 0)
  8835             {
  8836               pop_it (it);
  8837               if (it->method == GET_FROM_STRING)
  8838                 goto consider_string_end;
  8839             }
  8840         }
  8841       break;
  8842 
  8843     case GET_FROM_IMAGE:
  8844     case GET_FROM_STRETCH:
  8845     case GET_FROM_XWIDGET:
  8846 
  8847       /* The position etc with which we have to proceed are on
  8848          the stack.  The position may be at the end of a string,
  8849          if the `display' property takes up the whole string.  */
  8850       eassert (it->sp > 0);
  8851       pop_it (it);
  8852       if (it->method == GET_FROM_STRING)
  8853         goto consider_string_end;
  8854       break;
  8855 
  8856     default:
  8857       /* There are no other methods defined, so this should be a bug.  */
  8858       emacs_abort ();
  8859     }
  8860 
  8861   eassert (it->method != GET_FROM_STRING
  8862            || (STRINGP (it->string)
  8863                && IT_STRING_CHARPOS (*it) >= 0));
  8864 }
  8865 
  8866 /* Load IT's display element fields with information about the next
  8867    display element which comes from a display table entry or from the
  8868    result of translating a control character to one of the forms `^C'
  8869    or `\003'.
  8870 
  8871    IT->dpvec holds the glyphs to return as characters.
  8872    IT->saved_face_id holds the face id before the display vector--it
  8873    is restored into IT->face_id in set_iterator_to_next.  */
  8874 
  8875 static bool
  8876 next_element_from_display_vector (struct it *it)
  8877 {
  8878   Lisp_Object gc;
  8879   int prev_face_id = it->face_id;
  8880   int next_face_id;
  8881 
  8882   /* Precondition.  */
  8883   eassert (it->dpvec && it->current.dpvec_index >= 0);
  8884 
  8885   it->face_id = it->saved_face_id;
  8886 
  8887   /* KFS: This code used to check ip->dpvec[0] instead of the current element.
  8888      That seemed totally bogus - so I changed it...  */
  8889   if (it->dpend - it->dpvec > 0 /* empty dpvec[] is invalid */
  8890       && (gc = it->dpvec[it->current.dpvec_index], GLYPH_CODE_P (gc)))
  8891     {
  8892       struct face *this_face, *prev_face, *next_face;
  8893 
  8894       it->c = GLYPH_CODE_CHAR (gc);
  8895       it->len = CHAR_BYTES (it->c);
  8896 
  8897       /* The entry may contain a face id to use.  Such a face id is
  8898          the id of a Lisp face, not a realized face.  A face id of
  8899          zero means no face is specified.  */
  8900       if (it->dpvec_face_id >= 0)
  8901         it->face_id = it->dpvec_face_id;
  8902       else
  8903         {
  8904           int lface_id = GLYPH_CODE_FACE (gc);
  8905           if (lface_id > 0)
  8906             it->face_id = merge_faces (it->w, Qt, lface_id,
  8907                                        it->saved_face_id);
  8908         }
  8909 
  8910       /* Glyphs in the display vector could have the box face, so we
  8911          need to set the related flags in the iterator, as
  8912          appropriate.  */
  8913       this_face = FACE_FROM_ID_OR_NULL (it->f, it->face_id);
  8914       prev_face = FACE_FROM_ID_OR_NULL (it->f, prev_face_id);
  8915 
  8916       /* Is this character the first character of a box-face run?  */
  8917       it->start_of_box_run_p = (this_face && this_face->box != FACE_NO_BOX
  8918                                 && (!prev_face
  8919                                     || prev_face->box == FACE_NO_BOX));
  8920 
  8921       /* For the last character of the box-face run, we need to look
  8922          either at the next glyph from the display vector, or at the
  8923          face we saw before the display vector.  */
  8924       next_face_id = it->saved_face_id;
  8925       if (it->current.dpvec_index < it->dpend - it->dpvec - 1)
  8926         {
  8927           if (it->dpvec_face_id >= 0)
  8928             next_face_id = it->dpvec_face_id;
  8929           else
  8930             {
  8931               Lisp_Object gc = it->dpvec[it->current.dpvec_index + 1];
  8932               int lface_id = GLYPH_CODE_P (gc) ? GLYPH_CODE_FACE (gc) : 0;
  8933 
  8934               if (lface_id > 0)
  8935                 next_face_id = merge_faces (it->w, Qt, lface_id,
  8936                                             it->saved_face_id);
  8937             }
  8938         }
  8939       next_face = FACE_FROM_ID_OR_NULL (it->f, next_face_id);
  8940       if (this_face && this_face->box != FACE_NO_BOX
  8941           && (!next_face || next_face->box == FACE_NO_BOX))
  8942         it->end_of_box_run_p = true;
  8943       it->face_box_p = this_face && this_face->box != FACE_NO_BOX;
  8944     }
  8945   else
  8946     /* Display table entry is invalid.  Return a space.  */
  8947     it->c = ' ', it->len = 1;
  8948 
  8949   /* Don't change position and object of the iterator here.  They are
  8950      still the values of the character that had this display table
  8951      entry or was translated, and that's what we want.  */
  8952   it->what = IT_CHARACTER;
  8953   return true;
  8954 }
  8955 
  8956 /* Get the first element of string/buffer in the visual order, after
  8957    being reseated to a new position in a string or a buffer.  */
  8958 static void
  8959 get_visually_first_element (struct it *it)
  8960 {
  8961   bool string_p = STRINGP (it->string) || it->s;
  8962   ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
  8963   ptrdiff_t bob;
  8964   ptrdiff_t obegv = BEGV;
  8965 
  8966   SET_WITH_NARROWED_BEGV (it, bob,
  8967                           string_p ? 0 :
  8968                           IT_CHARPOS (*it) < BEGV ? obegv : BEGV,
  8969                           it->medium_narrowing_begv);
  8970 
  8971   if (STRINGP (it->string))
  8972     {
  8973       it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
  8974       it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
  8975     }
  8976   else
  8977     {
  8978       it->bidi_it.charpos = IT_CHARPOS (*it);
  8979       it->bidi_it.bytepos = IT_BYTEPOS (*it);
  8980     }
  8981 
  8982   if (it->bidi_it.charpos == eob)
  8983     {
  8984       /* Nothing to do, but reset the FIRST_ELT flag, like
  8985          bidi_paragraph_init does, because we are not going to
  8986          call it.  */
  8987       it->bidi_it.first_elt = false;
  8988     }
  8989   else if (it->bidi_it.charpos == bob
  8990            || (!string_p
  8991                && (FETCH_BYTE (it->bidi_it.bytepos - 1) == '\n'
  8992                    || FETCH_BYTE (it->bidi_it.bytepos) == '\n')))
  8993     {
  8994       /* If we are at the beginning of a line/string, we can produce
  8995          the next element right away.  */
  8996       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  8997       bidi_move_to_visually_next (&it->bidi_it);
  8998     }
  8999   else
  9000     {
  9001       ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
  9002 
  9003       /* We need to prime the bidi iterator starting at the line's or
  9004          string's beginning, before we will be able to produce the
  9005          next element.  */
  9006       if (string_p)
  9007         it->bidi_it.charpos = it->bidi_it.bytepos = 0;
  9008       else
  9009         SET_WITH_NARROWED_BEGV (it, it->bidi_it.charpos,
  9010                                 find_newline_no_quit (IT_CHARPOS (*it),
  9011                                                       IT_BYTEPOS (*it), -1,
  9012                                                       &it->bidi_it.bytepos),
  9013                                 it->medium_narrowing_begv);
  9014       bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, true);
  9015       do
  9016         {
  9017           /* Now return to buffer/string position where we were asked
  9018              to get the next display element, and produce that.  */
  9019           bidi_move_to_visually_next (&it->bidi_it);
  9020         }
  9021       while (it->bidi_it.bytepos != orig_bytepos
  9022              && it->bidi_it.charpos < eob);
  9023     }
  9024 
  9025   /*  Adjust IT's position information to where we ended up.  */
  9026   if (STRINGP (it->string))
  9027     {
  9028       IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
  9029       IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
  9030     }
  9031   else
  9032     {
  9033       IT_CHARPOS (*it) = it->bidi_it.charpos;
  9034       IT_BYTEPOS (*it) = it->bidi_it.bytepos;
  9035     }
  9036 
  9037   if (STRINGP (it->string) || !it->s)
  9038     {
  9039       ptrdiff_t stop, charpos, bytepos;
  9040 
  9041       if (STRINGP (it->string))
  9042         {
  9043           eassert (!it->s);
  9044           stop = SCHARS (it->string);
  9045           if (stop > it->end_charpos)
  9046             stop = it->end_charpos;
  9047           charpos = IT_STRING_CHARPOS (*it);
  9048           bytepos = IT_STRING_BYTEPOS (*it);
  9049         }
  9050       else
  9051         {
  9052           stop = it->end_charpos;
  9053           charpos = IT_CHARPOS (*it);
  9054           bytepos = IT_BYTEPOS (*it);
  9055         }
  9056       if (it->bidi_it.scan_dir < 0)
  9057         stop = -1;
  9058       composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
  9059                                     it->string, true);
  9060     }
  9061 }
  9062 
  9063 /* Load IT with the next display element from Lisp string IT->string.
  9064    IT->current.string_pos is the current position within the string.
  9065    If IT->current.overlay_string_index >= 0, the Lisp string is an
  9066    overlay string.  */
  9067 
  9068 static bool
  9069 next_element_from_string (struct it *it)
  9070 {
  9071   struct text_pos position;
  9072 
  9073   eassert (STRINGP (it->string));
  9074   eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
  9075   eassert (IT_STRING_CHARPOS (*it) >= 0);
  9076   position = it->current.string_pos;
  9077 
  9078   /* With bidi reordering, the character to display might not be the
  9079      character at IT_STRING_CHARPOS.  BIDI_IT.FIRST_ELT means
  9080      that we were reseat()ed to a new string, whose paragraph
  9081      direction is not known.  */
  9082   if (it->bidi_p && it->bidi_it.first_elt)
  9083     {
  9084       get_visually_first_element (it);
  9085       SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
  9086     }
  9087 
  9088   /* Time to check for invisible text?  */
  9089   if (IT_STRING_CHARPOS (*it) < it->end_charpos)
  9090     {
  9091       if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
  9092         {
  9093           if (!(!it->bidi_p
  9094                 || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9095                 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
  9096             {
  9097               /* With bidi non-linear iteration, we could find
  9098                  ourselves far beyond the last computed stop_charpos,
  9099                  with several other stop positions in between that we
  9100                  missed.  Scan them all now, in buffer's logical
  9101                  order, until we find and handle the last stop_charpos
  9102                  that precedes our current position.  */
  9103               handle_stop_backwards (it, it->stop_charpos);
  9104               return GET_NEXT_DISPLAY_ELEMENT (it);
  9105             }
  9106           else
  9107             {
  9108               if (it->bidi_p)
  9109                 {
  9110                   /* Take note of the stop position we just moved
  9111                      across, for when we will move back across it.  */
  9112                   it->prev_stop = it->stop_charpos;
  9113                   /* If we are at base paragraph embedding level, take
  9114                      note of the last stop position seen at this
  9115                      level.  */
  9116                   if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9117                     it->base_level_stop = it->stop_charpos;
  9118                 }
  9119               handle_stop (it);
  9120 
  9121               /* Since a handler may have changed IT->method, we must
  9122                  recurse here.  */
  9123               return GET_NEXT_DISPLAY_ELEMENT (it);
  9124             }
  9125         }
  9126       else if (it->bidi_p
  9127                /* If we are before prev_stop, we may have overstepped
  9128                   on our way backwards a stop_pos, and if so, we need
  9129                   to handle that stop_pos.  */
  9130                && IT_STRING_CHARPOS (*it) < it->prev_stop
  9131                /* We can sometimes back up for reasons that have nothing
  9132                   to do with bidi reordering.  E.g., compositions.  The
  9133                   code below is only needed when we are above the base
  9134                   embedding level, so test for that explicitly.  */
  9135                && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9136         {
  9137           /* If we lost track of base_level_stop, we have no better
  9138              place for handle_stop_backwards to start from than string
  9139              beginning.  This happens, e.g., when we were reseated to
  9140              the previous screenful of text by vertical-motion.  */
  9141           if (it->base_level_stop <= 0
  9142               || IT_STRING_CHARPOS (*it) < it->base_level_stop)
  9143             it->base_level_stop = 0;
  9144           handle_stop_backwards (it, it->base_level_stop);
  9145           return GET_NEXT_DISPLAY_ELEMENT (it);
  9146         }
  9147     }
  9148 
  9149   if (it->current.overlay_string_index >= 0)
  9150     {
  9151       /* Get the next character from an overlay string.  In overlay
  9152          strings, there is no field width or padding with spaces to
  9153          do.  */
  9154       if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
  9155         {
  9156           it->what = IT_EOB;
  9157           return false;
  9158         }
  9159       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9160                                 IT_STRING_BYTEPOS (*it),
  9161                                 it->bidi_it.scan_dir < 0
  9162                                 ? -1
  9163                                 : SCHARS (it->string))
  9164                && next_element_from_composition (it))
  9165         {
  9166           return true;
  9167         }
  9168       else if (STRING_MULTIBYTE (it->string))
  9169         {
  9170           const unsigned char *s = (SDATA (it->string)
  9171                                     + IT_STRING_BYTEPOS (*it));
  9172           it->c = check_char_and_length (s, &it->len);
  9173         }
  9174       else
  9175         {
  9176           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9177           it->len = 1;
  9178         }
  9179     }
  9180   else
  9181     {
  9182       /* Get the next character from a Lisp string that is not an
  9183          overlay string.  Such strings come from the mode line, for
  9184          example.  We may have to pad with spaces, or truncate the
  9185          string.  See also next_element_from_c_string.  */
  9186       if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
  9187         {
  9188           it->what = IT_EOB;
  9189           return false;
  9190         }
  9191       else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
  9192         {
  9193           /* Pad with spaces.  */
  9194           it->c = ' ', it->len = 1;
  9195           CHARPOS (position) = BYTEPOS (position) = -1;
  9196         }
  9197       else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
  9198                                 IT_STRING_BYTEPOS (*it),
  9199                                 it->bidi_it.scan_dir < 0
  9200                                 ? -1
  9201                                 : it->string_nchars)
  9202                && next_element_from_composition (it))
  9203         {
  9204           return true;
  9205         }
  9206       else if (STRING_MULTIBYTE (it->string))
  9207         {
  9208           const unsigned char *s = (SDATA (it->string)
  9209                                     + IT_STRING_BYTEPOS (*it));
  9210           it->c = check_char_and_length (s, &it->len);
  9211         }
  9212       else
  9213         {
  9214           it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
  9215           it->len = 1;
  9216         }
  9217     }
  9218 
  9219   /* Record what we have and where it came from.  */
  9220   it->what = IT_CHARACTER;
  9221   it->object = it->string;
  9222   it->position = position;
  9223   return true;
  9224 }
  9225 
  9226 
  9227 /* Load IT with next display element from C string IT->s.
  9228    IT->string_nchars is the maximum number of characters to return
  9229    from the string.  IT->end_charpos may be greater than
  9230    IT->string_nchars when this function is called, in which case we
  9231    may have to return padding spaces.  Value is false if end of string
  9232    reached, including padding spaces.  */
  9233 
  9234 static bool
  9235 next_element_from_c_string (struct it *it)
  9236 {
  9237   bool success_p = true;
  9238 
  9239   eassert (it->s);
  9240   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
  9241   it->what = IT_CHARACTER;
  9242   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
  9243   it->object = make_fixnum (0);
  9244 
  9245   /* With bidi reordering, the character to display might not be the
  9246      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9247      we were reseated to a new string, whose paragraph direction is
  9248      not known.  */
  9249   if (it->bidi_p && it->bidi_it.first_elt)
  9250     get_visually_first_element (it);
  9251 
  9252   /* IT's position can be greater than IT->string_nchars in case a
  9253      field width or precision has been specified when the iterator was
  9254      initialized.  */
  9255   if (IT_CHARPOS (*it) >= it->end_charpos)
  9256     {
  9257       /* End of the game.  */
  9258       it->what = IT_EOB;
  9259       success_p = false;
  9260     }
  9261   else if (IT_CHARPOS (*it) >= it->string_nchars)
  9262     {
  9263       /* Pad with spaces.  */
  9264       it->c = ' ', it->len = 1;
  9265       BYTEPOS (it->position) = CHARPOS (it->position) = -1;
  9266     }
  9267   else if (it->multibyte_p)
  9268     it->c = check_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
  9269   else
  9270     it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
  9271 
  9272   return success_p;
  9273 }
  9274 
  9275 
  9276 /* Set up IT to return characters from an ellipsis, if appropriate.
  9277    The definition of the ellipsis glyphs may come from a display table
  9278    entry.  This function fills IT with the first glyph from the
  9279    ellipsis if an ellipsis is to be displayed.  */
  9280 
  9281 static bool
  9282 next_element_from_ellipsis (struct it *it)
  9283 {
  9284   if (it->selective_display_ellipsis_p)
  9285     setup_for_ellipsis (it, it->len);
  9286   else
  9287     {
  9288       /* The face at the current position may be different from the
  9289          face we find after the invisible text.  Remember what it
  9290          was in IT->saved_face_id, and signal that it's there by
  9291          setting face_before_selective_p.  */
  9292       it->saved_face_id = it->face_id;
  9293       it->method = GET_FROM_BUFFER;
  9294       it->object = it->w->contents;
  9295       reseat_at_next_visible_line_start (it, true);
  9296       it->face_before_selective_p = true;
  9297     }
  9298 
  9299   return GET_NEXT_DISPLAY_ELEMENT (it);
  9300 }
  9301 
  9302 
  9303 /* Deliver an image display element.  The iterator IT is already
  9304    filled with image information (done in handle_display_prop).  Value
  9305    is always true.  */
  9306 
  9307 
  9308 static bool
  9309 next_element_from_image (struct it *it)
  9310 {
  9311   it->what = IT_IMAGE;
  9312   return true;
  9313 }
  9314 
  9315 static bool
  9316 next_element_from_xwidget (struct it *it)
  9317 {
  9318   it->what = IT_XWIDGET;
  9319   return true;
  9320 }
  9321 
  9322 
  9323 /* Fill iterator IT with next display element from a stretch glyph
  9324    property.  IT->object is the value of the text property.  Value is
  9325    always true.  */
  9326 
  9327 static bool
  9328 next_element_from_stretch (struct it *it)
  9329 {
  9330   it->what = IT_STRETCH;
  9331   return true;
  9332 }
  9333 
  9334 /* Scan backwards from IT's current position until we find a stop
  9335    position, or until BEGV.  This is called when we find ourself
  9336    before both the last known prev_stop and base_level_stop while
  9337    reordering bidirectional text.  */
  9338 
  9339 static void
  9340 compute_stop_pos_backwards (struct it *it)
  9341 {
  9342   const int SCAN_BACK_LIMIT = 1000;
  9343   struct text_pos pos;
  9344   struct display_pos save_current = it->current;
  9345   struct text_pos save_position = it->position;
  9346   ptrdiff_t charpos = IT_CHARPOS (*it);
  9347   ptrdiff_t where_we_are = charpos;
  9348   ptrdiff_t save_stop_pos = it->stop_charpos;
  9349   ptrdiff_t save_end_pos = it->end_charpos;
  9350 
  9351   eassert (NILP (it->string) && !it->s);
  9352   eassert (it->bidi_p);
  9353   it->bidi_p = false;
  9354   do
  9355     {
  9356       it->end_charpos = min (charpos + 1, ZV);
  9357       charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
  9358       SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
  9359       reseat_1 (it, pos, false);
  9360       compute_stop_pos (it);
  9361       /* We must advance forward, right?  */
  9362       if (it->stop_charpos <= charpos)
  9363         emacs_abort ();
  9364     }
  9365   while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
  9366 
  9367   if (it->stop_charpos <= where_we_are)
  9368     it->prev_stop = it->stop_charpos;
  9369   else
  9370     it->prev_stop = BEGV;
  9371   it->bidi_p = true;
  9372   it->current = save_current;
  9373   it->position = save_position;
  9374   it->stop_charpos = save_stop_pos;
  9375   it->end_charpos = save_end_pos;
  9376 }
  9377 
  9378 /* Scan forward from CHARPOS in the current buffer/string, until we
  9379    find a stop position > current IT's position.  Then handle the stop
  9380    position before that.  This is called when we bump into a stop
  9381    position while reordering bidirectional text.  CHARPOS should be
  9382    the last previously processed stop_pos (or BEGV/0, if none were
  9383    processed yet) whose position is less than IT's current
  9384    position.  */
  9385 
  9386 static void
  9387 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
  9388 {
  9389   bool bufp = !STRINGP (it->string);
  9390   ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
  9391   struct display_pos save_current = it->current;
  9392   struct text_pos save_position = it->position;
  9393   struct composition_it save_cmp_it = it->cmp_it;
  9394   struct text_pos pos1;
  9395   ptrdiff_t next_stop;
  9396 
  9397   /* Scan in strict logical order.  */
  9398   eassert (it->bidi_p);
  9399   it->bidi_p = false;
  9400   do
  9401     {
  9402       it->prev_stop = charpos;
  9403       if (bufp)
  9404         {
  9405           SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
  9406           reseat_1 (it, pos1, false);
  9407         }
  9408       else
  9409         it->current.string_pos = string_pos (charpos, it->string);
  9410       compute_stop_pos (it);
  9411       /* We must advance forward, right?  */
  9412       if (it->stop_charpos <= it->prev_stop)
  9413         emacs_abort ();
  9414       charpos = it->stop_charpos;
  9415     }
  9416   while (charpos <= where_we_are);
  9417 
  9418   it->bidi_p = true;
  9419   it->current = save_current;
  9420   it->position = save_position;
  9421   it->cmp_it = save_cmp_it;
  9422   next_stop = it->stop_charpos;
  9423   it->stop_charpos = it->prev_stop;
  9424   handle_stop (it);
  9425   it->stop_charpos = next_stop;
  9426 }
  9427 
  9428 /* Load IT with the next display element from current_buffer.  Value
  9429    is false if end of buffer reached.  IT->stop_charpos is the next
  9430    position at which to stop and check for text properties or buffer
  9431    end.  */
  9432 
  9433 static bool
  9434 next_element_from_buffer (struct it *it)
  9435 {
  9436   bool success_p = true;
  9437 
  9438   eassert (IT_CHARPOS (*it) >= BEGV);
  9439   eassert (NILP (it->string) && !it->s);
  9440   eassert (!it->bidi_p
  9441            || (NILP (it->bidi_it.string.lstring)
  9442                && it->bidi_it.string.s == NULL));
  9443 
  9444   /* With bidi reordering, the character to display might not be the
  9445      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT means that
  9446      we were reseat()ed to a new buffer position, which is potentially
  9447      a different paragraph.  */
  9448   if (it->bidi_p && it->bidi_it.first_elt)
  9449     {
  9450       get_visually_first_element (it);
  9451       SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9452     }
  9453 
  9454   if (IT_CHARPOS (*it) >= it->stop_charpos)
  9455     {
  9456       if (IT_CHARPOS (*it) >= it->end_charpos)
  9457         {
  9458           bool overlay_strings_follow_p;
  9459 
  9460           /* End of the game, except when overlay strings follow that
  9461              haven't been returned yet.  */
  9462           if (it->overlay_strings_at_end_processed_p)
  9463             overlay_strings_follow_p = false;
  9464           else
  9465             {
  9466               it->overlay_strings_at_end_processed_p = true;
  9467               overlay_strings_follow_p = get_overlay_strings (it, 0);
  9468             }
  9469 
  9470           if (overlay_strings_follow_p)
  9471             success_p = GET_NEXT_DISPLAY_ELEMENT (it);
  9472           else
  9473             {
  9474               it->what = IT_EOB;
  9475               it->position = it->current.pos;
  9476               success_p = false;
  9477             }
  9478         }
  9479       else if (!(!it->bidi_p
  9480                  || BIDI_AT_BASE_LEVEL (it->bidi_it)
  9481                  || IT_CHARPOS (*it) == it->stop_charpos))
  9482         {
  9483           /* With bidi non-linear iteration, we could find ourselves
  9484              far beyond the last computed stop_charpos, with several
  9485              other stop positions in between that we missed.  Scan
  9486              them all now, in buffer's logical order, until we find
  9487              and handle the last stop_charpos that precedes our
  9488              current position.  */
  9489           handle_stop_backwards (it, it->stop_charpos);
  9490           it->ignore_overlay_strings_at_pos_p = false;
  9491           return GET_NEXT_DISPLAY_ELEMENT (it);
  9492         }
  9493       else
  9494         {
  9495           if (it->bidi_p)
  9496             {
  9497               /* Take note of the stop position we just moved across,
  9498                  for when we will move back across it.  */
  9499               it->prev_stop = it->stop_charpos;
  9500               /* If we are at base paragraph embedding level, take
  9501                  note of the last stop position seen at this
  9502                  level.  */
  9503               if (BIDI_AT_BASE_LEVEL (it->bidi_it))
  9504                 it->base_level_stop = it->stop_charpos;
  9505             }
  9506           handle_stop (it);
  9507           it->ignore_overlay_strings_at_pos_p = false;
  9508           return GET_NEXT_DISPLAY_ELEMENT (it);
  9509         }
  9510     }
  9511   else if (it->bidi_p
  9512            /* If we are before prev_stop, we may have overstepped on
  9513               our way backwards a stop_pos, and if so, we need to
  9514               handle that stop_pos.  */
  9515            && IT_CHARPOS (*it) < it->prev_stop
  9516            /* We can sometimes back up for reasons that have nothing
  9517               to do with bidi reordering.  E.g., compositions.  The
  9518               code below is only needed when we are above the base
  9519               embedding level, so test for that explicitly.  */
  9520            && !BIDI_AT_BASE_LEVEL (it->bidi_it))
  9521     {
  9522       if (it->base_level_stop <= 0
  9523           || IT_CHARPOS (*it) < it->base_level_stop)
  9524         {
  9525           /* If we lost track of base_level_stop, we need to find
  9526              prev_stop by looking backwards.  This happens, e.g., when
  9527              we were reseated to the previous screenful of text by
  9528              vertical-motion.  */
  9529           it->base_level_stop = BEGV;
  9530           compute_stop_pos_backwards (it);
  9531           handle_stop_backwards (it, it->prev_stop);
  9532         }
  9533       else
  9534         handle_stop_backwards (it, it->base_level_stop);
  9535       it->ignore_overlay_strings_at_pos_p = false;
  9536       return GET_NEXT_DISPLAY_ELEMENT (it);
  9537     }
  9538   else
  9539     {
  9540       /* No face changes, overlays etc. in sight, so just return a
  9541          character from current_buffer.  */
  9542       unsigned char *p;
  9543       ptrdiff_t stop;
  9544 
  9545       /* We moved to the next buffer position, so any info about
  9546          previously seen overlays is no longer valid.  */
  9547       it->ignore_overlay_strings_at_pos_p = false;
  9548 
  9549       if (composition_break_at_point
  9550           && !NILP (BVAR (current_buffer, enable_multibyte_characters))
  9551           && !NILP (Vauto_composition_mode))
  9552         {
  9553           /* Limit search for composable characters to point's position.  */
  9554           if (it->bidi_it.scan_dir < 0)
  9555             stop = (PT <= IT_CHARPOS (*it)) ? PT : -1;
  9556           else
  9557             stop = (IT_CHARPOS (*it) < PT
  9558                     && PT < it->end_charpos) ? PT : it->end_charpos;
  9559         }
  9560       else
  9561         stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
  9562       if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
  9563                            stop)
  9564           && next_element_from_composition (it))
  9565         {
  9566           return true;
  9567         }
  9568 
  9569       /* Get the next character, maybe multibyte.  */
  9570       p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
  9571       if (it->multibyte_p && !ASCII_CHAR_P (*p))
  9572         it->c = string_char_and_length (p, &it->len);
  9573       else
  9574         it->c = *p, it->len = 1;
  9575 
  9576       /* Record what we have and where it came from.  */
  9577       it->what = IT_CHARACTER;
  9578       it->object = it->w->contents;
  9579       it->position = it->current.pos;
  9580 
  9581       /* Normally we return the character found above, except when we
  9582          really want to return an ellipsis for selective display.  */
  9583       if (it->selective)
  9584         {
  9585           if (it->c == '\n')
  9586             {
  9587               /* A value of selective > 0 means hide lines indented more
  9588                  than that number of columns.  */
  9589               if (it->selective > 0
  9590                   && IT_CHARPOS (*it) + 1 < ZV
  9591                   && indented_beyond_p (IT_CHARPOS (*it) + 1,
  9592                                         IT_BYTEPOS (*it) + 1,
  9593                                         it->selective))
  9594                 {
  9595                   success_p = next_element_from_ellipsis (it);
  9596                   it->dpvec_char_len = -1;
  9597                 }
  9598             }
  9599           else if (it->c == '\r' && it->selective == -1)
  9600             {
  9601               /* A value of selective == -1 means that everything from the
  9602                  CR to the end of the line is invisible, with maybe an
  9603                  ellipsis displayed for it.  */
  9604               success_p = next_element_from_ellipsis (it);
  9605               it->dpvec_char_len = -1;
  9606             }
  9607         }
  9608     }
  9609 
  9610   /* Value is false if end of buffer reached.  */
  9611   eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
  9612   return success_p;
  9613 }
  9614 
  9615 
  9616 /* Deliver a composition display element.  Unlike the other
  9617    next_element_from_XXX, this function is not registered in the array
  9618    get_next_element[].  It is called from next_element_from_buffer and
  9619    next_element_from_string when necessary.  */
  9620 
  9621 static bool
  9622 next_element_from_composition (struct it *it)
  9623 {
  9624   it->what = IT_COMPOSITION;
  9625   it->len = it->cmp_it.nbytes;
  9626   if (STRINGP (it->string))
  9627     {
  9628       if (it->c < 0)
  9629         {
  9630           IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
  9631           IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
  9632           return false;
  9633         }
  9634       it->position = it->current.string_pos;
  9635       it->object = it->string;
  9636       it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
  9637                                      IT_STRING_BYTEPOS (*it), it->string);
  9638     }
  9639   else
  9640     {
  9641       if (it->c < 0)
  9642         {
  9643           IT_CHARPOS (*it) += it->cmp_it.nchars;
  9644           IT_BYTEPOS (*it) += it->cmp_it.nbytes;
  9645           if (it->bidi_p)
  9646             {
  9647               if (it->bidi_it.new_paragraph)
  9648                 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it,
  9649                                      false);
  9650               /* Resync the bidi iterator with IT's new position.
  9651                  FIXME: this doesn't support bidirectional text.  */
  9652               while (it->bidi_it.charpos < IT_CHARPOS (*it))
  9653                 bidi_move_to_visually_next (&it->bidi_it);
  9654             }
  9655           return false;
  9656         }
  9657       it->position = it->current.pos;
  9658       it->object = it->w->contents;
  9659       it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
  9660                                      IT_BYTEPOS (*it), Qnil);
  9661     }
  9662   return true;
  9663 }
  9664 
  9665 
  9666 
  9667 /***********************************************************************
  9668              Moving an iterator without producing glyphs
  9669  ***********************************************************************/
  9670 
  9671 /* Check if iterator is at a position corresponding to a valid buffer
  9672    position after some move_it_ call.  */
  9673 
  9674 #define IT_POS_VALID_AFTER_MOVE_P(it)                   \
  9675   ((it)->method != GET_FROM_STRING || IT_STRING_CHARPOS (*it) == 0)
  9676 
  9677 
  9678 /* Move iterator IT to a specified buffer or X position within one
  9679    line on the display without producing glyphs.
  9680 
  9681    OP should be a bit mask including some or all of these bits:
  9682     MOVE_TO_X: Stop upon reaching x-position TO_X.
  9683     MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
  9684    Regardless of OP's value, stop upon reaching the end of the display line.
  9685 
  9686    TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
  9687    This means, in particular, that TO_X includes window's horizontal
  9688    scroll amount.
  9689 
  9690    The return value has several possible values that
  9691    say what condition caused the scan to stop:
  9692 
  9693    MOVE_POS_MATCH_OR_ZV
  9694      - when TO_POS or ZV was reached.
  9695 
  9696    MOVE_X_REACHED
  9697      -when TO_X was reached before TO_POS or ZV were reached.
  9698 
  9699    MOVE_LINE_CONTINUED
  9700      - when we reached the end of the display area and the line must
  9701      be continued.
  9702 
  9703    MOVE_LINE_TRUNCATED
  9704      - when we reached the end of the display area and the line is
  9705      truncated.
  9706 
  9707    MOVE_NEWLINE_OR_CR
  9708      - when we stopped at a line end, i.e. a newline or a CR and selective
  9709      display is on.  */
  9710 
  9711 static enum move_it_result
  9712 move_it_in_display_line_to (struct it *it,
  9713                             ptrdiff_t to_charpos, int to_x,
  9714                             enum move_operation_enum op)
  9715 {
  9716   enum move_it_result result = MOVE_UNDEFINED;
  9717   struct glyph_row *saved_glyph_row;
  9718   struct it wrap_it, atpos_it, atx_it, ppos_it;
  9719   void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
  9720   void *ppos_data = NULL;
  9721   bool may_wrap = false;
  9722   enum it_method prev_method = it->method;
  9723   ptrdiff_t closest_pos UNINIT;
  9724   ptrdiff_t prev_pos = IT_CHARPOS (*it);
  9725   bool saw_smaller_pos = prev_pos < to_charpos;
  9726   bool line_number_pending = false;
  9727 
  9728   /* Don't produce glyphs in produce_glyphs.  */
  9729   saved_glyph_row = it->glyph_row;
  9730   it->glyph_row = NULL;
  9731 
  9732   /* Use wrap_it to save a copy of IT wherever a word wrap could
  9733      occur.  Use atpos_it to save a copy of IT at the desired buffer
  9734      position, if found, so that we can scan ahead and check if the
  9735      word later overshoots the window edge.  Use atx_it similarly, for
  9736      pixel positions.  */
  9737   wrap_it.sp = -1;
  9738   atpos_it.sp = -1;
  9739   atx_it.sp = -1;
  9740 
  9741   /* Use ppos_it under bidi reordering to save a copy of IT for the
  9742      initial position.  We restore that position in IT when we have
  9743      scanned the entire display line without finding a match for
  9744      TO_CHARPOS and all the character positions are greater than
  9745      TO_CHARPOS.  We then restart the scan from the initial position,
  9746      and stop at CLOSEST_POS, which is a position > TO_CHARPOS that is
  9747      the closest to TO_CHARPOS.  */
  9748   if (it->bidi_p)
  9749     {
  9750       if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
  9751         {
  9752           SAVE_IT (ppos_it, *it, ppos_data);
  9753           closest_pos = IT_CHARPOS (*it);
  9754         }
  9755       else
  9756         closest_pos = ZV;
  9757     }
  9758 
  9759 #define BUFFER_POS_REACHED_P()                                  \
  9760   ((op & MOVE_TO_POS) != 0                                      \
  9761    && BUFFERP (it->object)                                      \
  9762    && (IT_CHARPOS (*it) == to_charpos                           \
  9763        || ((!it->bidi_p                                         \
  9764             || BIDI_AT_BASE_LEVEL (it->bidi_it))                \
  9765            && IT_CHARPOS (*it) > to_charpos)                    \
  9766        || (it->what == IT_COMPOSITION                           \
  9767            && ((IT_CHARPOS (*it) > to_charpos                   \
  9768                 && to_charpos >= it->cmp_it.charpos)            \
  9769                || (IT_CHARPOS (*it) < to_charpos                \
  9770                    && to_charpos <= it->cmp_it.charpos))))      \
  9771    && (it->method == GET_FROM_BUFFER                            \
  9772        || (it->method == GET_FROM_DISPLAY_VECTOR                \
  9773            && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
  9774 
  9775   if (it->hpos == 0)
  9776     {
  9777       /* If line numbers are being displayed, produce a line number.
  9778          But don't do that if we are to reach first_visible_x, because
  9779          line numbers are not relevant to stuff that is not visible on
  9780          display.  */
  9781       if (!((op && MOVE_TO_X) && to_x == it->first_visible_x)
  9782           && should_produce_line_number (it))
  9783         {
  9784           if (it->current_x == it->first_visible_x)
  9785             maybe_produce_line_number (it);
  9786           else
  9787             line_number_pending = true;
  9788         }
  9789       /* If there's a line-/wrap-prefix, handle it, if we didn't already.  */
  9790       if (it->area == TEXT_AREA && !it->string_from_prefix_prop_p)
  9791         handle_line_prefix (it);
  9792     }
  9793 
  9794   if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9795     SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9796 
  9797   while (true)
  9798     {
  9799       int x, i, ascent = 0, descent = 0;
  9800 
  9801 /* Utility macro to reset an iterator with x, ascent, and descent.  */
  9802 #define IT_RESET_X_ASCENT_DESCENT(IT)                   \
  9803   ((IT)->current_x = x, (IT)->max_ascent = ascent,      \
  9804    (IT)->max_descent = descent)
  9805 
  9806       /* Stop if we move beyond TO_CHARPOS (after an image or a
  9807          display string or stretch glyph).  */
  9808       if ((op & MOVE_TO_POS) != 0
  9809           && BUFFERP (it->object)
  9810           && it->method == GET_FROM_BUFFER
  9811           && (((!it->bidi_p
  9812                 /* When the iterator is at base embedding level, we
  9813                    are guaranteed that characters are delivered for
  9814                    display in strictly increasing order of their
  9815                    buffer positions.  */
  9816                 || BIDI_AT_BASE_LEVEL (it->bidi_it))
  9817                && IT_CHARPOS (*it) > to_charpos)
  9818               || (it->bidi_p
  9819                   && (prev_method == GET_FROM_IMAGE
  9820                       || prev_method == GET_FROM_STRETCH
  9821                       || prev_method == GET_FROM_STRING)
  9822                   /* Passed TO_CHARPOS from left to right.  */
  9823                   && ((prev_pos < to_charpos
  9824                        && IT_CHARPOS (*it) >= to_charpos)
  9825                       /* Passed TO_CHARPOS from right to left.  */
  9826                       || (prev_pos > to_charpos
  9827                           && IT_CHARPOS (*it) <= to_charpos)))))
  9828         {
  9829           if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9830             {
  9831               result = MOVE_POS_MATCH_OR_ZV;
  9832               break;
  9833             }
  9834           else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
  9835             /* If wrap_it is valid, the current position might be in a
  9836                word that is wrapped.  So, save the iterator in
  9837                atpos_it and continue to see if wrapping happens.  */
  9838             SAVE_IT (atpos_it, *it, atpos_data);
  9839         }
  9840 
  9841       /* Stop when ZV reached.
  9842          We used to stop here when TO_CHARPOS reached as well, but that is
  9843          too soon if this glyph does not fit on this line.  So we handle it
  9844          explicitly below.  */
  9845       if (!get_next_display_element (it))
  9846         {
  9847           result = MOVE_POS_MATCH_OR_ZV;
  9848           break;
  9849         }
  9850 
  9851       if (it->line_wrap == TRUNCATE)
  9852         {
  9853           /* If it->pixel_width is zero, the last PRODUCE_GLYPHS call
  9854              produced something that doesn't consume any screen estate
  9855              in the text area, so we don't want to exit the loop at
  9856              TO_CHARPOS, before we produce the glyph for that buffer
  9857              position.  This happens, e.g., when there's an overlay at
  9858              TO_CHARPOS that draws a fringe bitmap.  */
  9859           if (BUFFER_POS_REACHED_P ()
  9860               && (it->pixel_width > 0
  9861                   || IT_CHARPOS (*it) > to_charpos
  9862                   || it->area != TEXT_AREA))
  9863             {
  9864               result = MOVE_POS_MATCH_OR_ZV;
  9865               break;
  9866             }
  9867         }
  9868       else
  9869         {
  9870           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
  9871             {
  9872               bool next_may_wrap = may_wrap;
  9873               /* Can we wrap after this character?  */
  9874               if (char_can_wrap_after (it))
  9875                 next_may_wrap = true;
  9876               else
  9877                 next_may_wrap = false;
  9878               /* Can we wrap here? */
  9879               if (may_wrap && char_can_wrap_before (it))
  9880                 {
  9881                   /* We have reached a glyph that follows one or more
  9882                      whitespace characters or characters that allow
  9883                      wrapping after them.  If this character allows
  9884                      wrapping before it, save this position as a
  9885                      wrapping point.  */
  9886                   if (atpos_it.sp >= 0)
  9887                     {
  9888                       RESTORE_IT (it, &atpos_it, atpos_data);
  9889                       result = MOVE_POS_MATCH_OR_ZV;
  9890                       goto done;
  9891                     }
  9892                   if (atx_it.sp >= 0)
  9893                     {
  9894                       RESTORE_IT (it, &atx_it, atx_data);
  9895                       result = MOVE_X_REACHED;
  9896                       goto done;
  9897                     }
  9898                   /* Otherwise, we can wrap here.  */
  9899                   SAVE_IT (wrap_it, *it, wrap_data);
  9900                 }
  9901               /* Update may_wrap for the next iteration.  */
  9902               may_wrap = next_may_wrap;
  9903             }
  9904         }
  9905 
  9906       /* Remember the line height for the current line, in case
  9907          the next element doesn't fit on the line.  */
  9908       ascent = it->max_ascent;
  9909       descent = it->max_descent;
  9910 
  9911       /* The call to produce_glyphs will get the metrics of the
  9912          display element IT is loaded with.  Record the x-position
  9913          before this display element, in case it doesn't fit on the
  9914          line.  */
  9915       x = it->current_x;
  9916 
  9917       PRODUCE_GLYPHS (it);
  9918 
  9919       if (it->area != TEXT_AREA)
  9920         {
  9921           prev_method = it->method;
  9922           if (it->method == GET_FROM_BUFFER)
  9923             prev_pos = IT_CHARPOS (*it);
  9924           set_iterator_to_next (it, true);
  9925           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
  9926             SET_TEXT_POS (this_line_min_pos,
  9927                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
  9928           if (it->bidi_p
  9929               && (op & MOVE_TO_POS)
  9930               && IT_CHARPOS (*it) > to_charpos
  9931               && IT_CHARPOS (*it) < closest_pos)
  9932             closest_pos = IT_CHARPOS (*it);
  9933           continue;
  9934         }
  9935 
  9936       /* The number of glyphs we get back in IT->nglyphs will normally
  9937          be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
  9938          character on a terminal frame, or (iii) a line end.  For the
  9939          second case, IT->nglyphs - 1 padding glyphs will be present.
  9940          (On X frames, there is only one glyph produced for a
  9941          composite character.)
  9942 
  9943          The behavior implemented below means, for continuation lines,
  9944          that as many spaces of a TAB as fit on the current line are
  9945          displayed there.  For terminal frames, as many glyphs of a
  9946          multi-glyph character are displayed in the current line, too.
  9947          This is what the old redisplay code did, and we keep it that
  9948          way.  Under X, the whole shape of a complex character must
  9949          fit on the line or it will be completely displayed in the
  9950          next line.
  9951 
  9952          Note that both for tabs and padding glyphs, all glyphs have
  9953          the same width.  */
  9954       if (it->nglyphs)
  9955         {
  9956           /* More than one glyph or glyph doesn't fit on line.  All
  9957              glyphs have the same width.  */
  9958           int single_glyph_width = it->pixel_width / it->nglyphs;
  9959           int new_x;
  9960           int x_before_this_char = x;
  9961           int hpos_before_this_char = it->hpos;
  9962 
  9963           for (i = 0; i < it->nglyphs; ++i, x = new_x)
  9964             {
  9965               new_x = x + single_glyph_width;
  9966 
  9967               /* We want to leave anything reaching TO_X to the caller.  */
  9968               if ((op & MOVE_TO_X) && new_x > to_x)
  9969                 {
  9970                   if (BUFFER_POS_REACHED_P ())
  9971                     {
  9972                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9973                         goto buffer_pos_reached;
  9974                       if (atpos_it.sp < 0)
  9975                         {
  9976                           SAVE_IT (atpos_it, *it, atpos_data);
  9977                           IT_RESET_X_ASCENT_DESCENT (&atpos_it);
  9978                         }
  9979                     }
  9980                   else
  9981                     {
  9982                       if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
  9983                         {
  9984                           it->current_x = x;
  9985                           result = MOVE_X_REACHED;
  9986                           break;
  9987                         }
  9988                       if (atx_it.sp < 0)
  9989                         {
  9990                           SAVE_IT (atx_it, *it, atx_data);
  9991                           IT_RESET_X_ASCENT_DESCENT (&atx_it);
  9992                         }
  9993                     }
  9994                 }
  9995 
  9996               if (/* Lines are continued.  */
  9997                   it->line_wrap != TRUNCATE
  9998                   && (/* And glyph doesn't fit on the line.  */
  9999                       new_x > it->last_visible_x
 10000                       /* Or it fits exactly and we're on a window
 10001                          system frame.  */
 10002                       || (new_x == it->last_visible_x
 10003                           && FRAME_WINDOW_P (it->f)
 10004                           && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10005                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10006                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 10007                 {
 10008                   bool moved_forward = false;
 10009 
 10010                   if (/* IT->hpos == 0 means the very first glyph
 10011                          doesn't fit on the line, e.g. a wide image.  */
 10012                       it->hpos == 0
 10013                       || (new_x == it->last_visible_x
 10014                           && FRAME_WINDOW_P (it->f)))
 10015                     {
 10016                       ++it->hpos;
 10017                       it->current_x = new_x;
 10018 
 10019                       /* The character's last glyph just barely fits
 10020                          in this row.  */
 10021                       if (i == it->nglyphs - 1)
 10022                         {
 10023                           /* If this is the destination position,
 10024                              return a position *before* it in this row,
 10025                              now that we know it fits in this row.  */
 10026                           if (BUFFER_POS_REACHED_P ())
 10027                             {
 10028                               bool can_wrap = true;
 10029 
 10030                               /* If the previous character says we can
 10031                                  wrap after it, but the current
 10032                                  character says we can't wrap before
 10033                                  it, then we can't wrap here.  */
 10034                               if (it->line_wrap == WORD_WRAP
 10035                                   && wrap_it.sp >= 0
 10036                                   && may_wrap
 10037                                   && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10038                                 {
 10039                                   struct it tem_it;
 10040                                   void *tem_data = NULL;
 10041 
 10042                                   SAVE_IT (tem_it, *it, tem_data);
 10043                                   set_iterator_to_next (it, true);
 10044                                   if (get_next_display_element (it)
 10045                                       && !char_can_wrap_before (it))
 10046                                     can_wrap = false;
 10047                                   RESTORE_IT (it, &tem_it, tem_data);
 10048                                 }
 10049                               if (it->line_wrap != WORD_WRAP
 10050                                   || wrap_it.sp < 0
 10051                                   /* If we've just found whitespace
 10052                                      where we can wrap, effectively
 10053                                      ignore the previous wrap point --
 10054                                      it is no longer relevant, but we
 10055                                      won't have an opportunity to
 10056                                      update it, since we've reached
 10057                                      the edge of this screen line.  */
 10058                                   || (may_wrap && can_wrap
 10059                                       && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10060                                 {
 10061                                   it->hpos = hpos_before_this_char;
 10062                                   it->current_x = x_before_this_char;
 10063                                   result = MOVE_POS_MATCH_OR_ZV;
 10064                                   break;
 10065                                 }
 10066                               if (it->line_wrap == WORD_WRAP
 10067                                   && atpos_it.sp < 0)
 10068                                 {
 10069                                   SAVE_IT (atpos_it, *it, atpos_data);
 10070                                   atpos_it.current_x = x_before_this_char;
 10071                                   atpos_it.hpos = hpos_before_this_char;
 10072                                 }
 10073                             }
 10074 
 10075                           prev_method = it->method;
 10076                           if (it->method == GET_FROM_BUFFER)
 10077                             prev_pos = IT_CHARPOS (*it);
 10078                           set_iterator_to_next (it, true);
 10079                           if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10080                             SET_TEXT_POS (this_line_min_pos,
 10081                                           IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10082                           /* On graphical terminals, newlines may
 10083                              "overflow" into the fringe if
 10084                              overflow-newline-into-fringe is non-nil.
 10085                              On text terminals, and on graphical
 10086                              terminals with no right margin, newlines
 10087                              may overflow into the last glyph on the
 10088                              display line.*/
 10089                           if (!FRAME_WINDOW_P (it->f)
 10090                               || ((it->bidi_p
 10091                                    && it->bidi_it.paragraph_dir == R2L)
 10092                                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10093                                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10094                               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10095                             {
 10096                               if (!get_next_display_element (it))
 10097                                 {
 10098                                   result = MOVE_POS_MATCH_OR_ZV;
 10099                                   break;
 10100                                 }
 10101                               moved_forward = true;
 10102                               if (BUFFER_POS_REACHED_P ())
 10103                                 {
 10104                                   if (ITERATOR_AT_END_OF_LINE_P (it))
 10105                                     result = MOVE_POS_MATCH_OR_ZV;
 10106                                   else
 10107                                     result = MOVE_LINE_CONTINUED;
 10108                                   break;
 10109                                 }
 10110                               if (ITERATOR_AT_END_OF_LINE_P (it)
 10111                                   && (it->line_wrap != WORD_WRAP
 10112                                       || wrap_it.sp < 0
 10113                                       || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
 10114                                 {
 10115                                   result = MOVE_NEWLINE_OR_CR;
 10116                                   break;
 10117                                 }
 10118                             }
 10119                         }
 10120                     }
 10121                   else
 10122                     IT_RESET_X_ASCENT_DESCENT (it);
 10123 
 10124                   /* If the screen line ends with whitespace (or
 10125                      wrap-able character), and we are under word-wrap,
 10126                      don't use wrap_it: it is no longer relevant, but
 10127                      we won't have an opportunity to update it, since
 10128                      we are done with this screen line.  */
 10129                   if (may_wrap && IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 10130                       /* If the character after the one which set the
 10131                          may_wrap flag says we can't wrap before it,
 10132                          we can't wrap here.  Therefore, wrap_it
 10133                          (previously found wrap-point) _is_ relevant
 10134                          in that case.  */
 10135                       && (!moved_forward || char_can_wrap_before (it)))
 10136                     {
 10137                       /* If we've found TO_X, go back there, as we now
 10138                          know the last word fits on this screen line.  */
 10139                       if ((op & MOVE_TO_X) && new_x == it->last_visible_x
 10140                           && atx_it.sp >= 0)
 10141                         {
 10142                           RESTORE_IT (it, &atx_it, atx_data);
 10143                           atpos_it.sp = -1;
 10144                           atx_it.sp = -1;
 10145                           result = MOVE_X_REACHED;
 10146                           break;
 10147                         }
 10148                     }
 10149                   else if (wrap_it.sp >= 0)
 10150                     {
 10151                       RESTORE_IT (it, &wrap_it, wrap_data);
 10152                       atpos_it.sp = -1;
 10153                       atx_it.sp = -1;
 10154                     }
 10155 
 10156                   move_trace ("move_it_in: continued at %td\n",
 10157                               IT_CHARPOS (*it));
 10158                   result = MOVE_LINE_CONTINUED;
 10159                   break;
 10160                 }
 10161 
 10162               if (BUFFER_POS_REACHED_P ())
 10163                 {
 10164                   if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
 10165                     goto buffer_pos_reached;
 10166                   if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
 10167                     {
 10168                       SAVE_IT (atpos_it, *it, atpos_data);
 10169                       IT_RESET_X_ASCENT_DESCENT (&atpos_it);
 10170                     }
 10171                 }
 10172 
 10173               if (new_x > it->first_visible_x)
 10174                 {
 10175                   /* If we have reached the visible portion of the
 10176                      screen line, produce the line number if needed.  */
 10177                   if (line_number_pending)
 10178                     {
 10179                       line_number_pending = false;
 10180                       it->current_x = it->first_visible_x;
 10181                       maybe_produce_line_number (it);
 10182                       it->current_x += new_x - it->first_visible_x;
 10183                     }
 10184                   /* Glyph is visible.  Increment number of glyphs that
 10185                      would be displayed.  */
 10186                   ++it->hpos;
 10187                 }
 10188             }
 10189 
 10190           if (result != MOVE_UNDEFINED)
 10191             break;
 10192         }
 10193       else if (BUFFER_POS_REACHED_P ())
 10194         {
 10195         buffer_pos_reached:
 10196           IT_RESET_X_ASCENT_DESCENT (it);
 10197           result = MOVE_POS_MATCH_OR_ZV;
 10198           break;
 10199         }
 10200       else if ((op & MOVE_TO_X) && it->current_x >= to_x)
 10201         {
 10202           /* Stop when TO_X specified and reached.  This check is
 10203              necessary here because of lines consisting of a line end,
 10204              only.  The line end will not produce any glyphs and we
 10205              would never get MOVE_X_REACHED.  */
 10206           eassert (it->nglyphs == 0);
 10207           result = MOVE_X_REACHED;
 10208           break;
 10209         }
 10210 
 10211       /* Is this a line end?  If yes, we're done.  */
 10212       if (ITERATOR_AT_END_OF_LINE_P (it))
 10213         {
 10214           /* If we are past TO_CHARPOS, but never saw any character
 10215              positions smaller than TO_CHARPOS, return
 10216              MOVE_POS_MATCH_OR_ZV, like the unidirectional display
 10217              did.  */
 10218           if (it->bidi_p && (op & MOVE_TO_POS) != 0)
 10219             {
 10220               if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
 10221                 {
 10222                   if (closest_pos < ZV)
 10223                     {
 10224                       RESTORE_IT (it, &ppos_it, ppos_data);
 10225                       /* Don't recurse if closest_pos is equal to
 10226                          to_charpos, since we have just tried that.  */
 10227                       if (closest_pos != to_charpos)
 10228                         move_it_in_display_line_to (it, closest_pos, -1,
 10229                                                     MOVE_TO_POS);
 10230                       result = MOVE_POS_MATCH_OR_ZV;
 10231                     }
 10232                   else
 10233                     goto buffer_pos_reached;
 10234                 }
 10235               else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
 10236                        && IT_CHARPOS (*it) > to_charpos)
 10237                 goto buffer_pos_reached;
 10238               else
 10239                 result = MOVE_NEWLINE_OR_CR;
 10240             }
 10241           else
 10242             result = MOVE_NEWLINE_OR_CR;
 10243           /* If lines are truncated, and the line we moved across is
 10244              completely hscrolled out of view, reset the line metrics
 10245              to those of the newline we've just processed, so that
 10246              glyphs not on display don't affect the line's height.  */
 10247           if (it->line_wrap == TRUNCATE
 10248               && it->current_x <= it->first_visible_x
 10249               && result == MOVE_NEWLINE_OR_CR
 10250               && it->char_to_display == '\n')
 10251             {
 10252               it->max_ascent = it->ascent;
 10253               it->max_descent = it->descent;
 10254             }
 10255           /* If we've processed the newline, make sure this flag is
 10256              reset, as it must only be set when the newline itself is
 10257              processed.  */
 10258           if (result == MOVE_NEWLINE_OR_CR)
 10259             it->constrain_row_ascent_descent_p = false;
 10260           break;
 10261         }
 10262 
 10263       prev_method = it->method;
 10264       if (it->method == GET_FROM_BUFFER)
 10265         prev_pos = IT_CHARPOS (*it);
 10266 
 10267       /* Detect overly-wide wrap-prefixes made of (space ...) display
 10268          properties.  When such a wrap prefix reaches past the right
 10269          margin of the window, we need to avoid the call to
 10270          set_iterator_to_next below, so that it->line_wrap is left at
 10271          its TRUNCATE value wisely set by handle_line_prefix.
 10272          Otherwise, set_iterator_to_next will pop the iterator stack,
 10273          restore it->line_wrap, and we might miss the opportunity to
 10274          exit the loop and return.  */
 10275       bool overwide_wrap_prefix =
 10276         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 10277         && it->sp > 0 && it->method == GET_FROM_STRETCH
 10278         && it->current_x >= it->last_visible_x
 10279         && it->continuation_lines_width > 0
 10280         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 10281       /* The current display element has been consumed.  Advance
 10282          to the next.  */
 10283       if (!overwide_wrap_prefix)
 10284         set_iterator_to_next (it, true);
 10285       if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
 10286         SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
 10287       if (IT_CHARPOS (*it) < to_charpos)
 10288         saw_smaller_pos = true;
 10289       if (it->bidi_p
 10290           && (op & MOVE_TO_POS)
 10291           && IT_CHARPOS (*it) >= to_charpos
 10292           && IT_CHARPOS (*it) < closest_pos)
 10293         closest_pos = IT_CHARPOS (*it);
 10294 
 10295       /* Stop if lines are truncated and IT's current x-position is
 10296          past the right edge of the window now.  */
 10297       if (it->line_wrap == TRUNCATE
 10298           && it->current_x >= it->last_visible_x)
 10299         {
 10300           if (!FRAME_WINDOW_P (it->f)
 10301               || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 10302                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 10303                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
 10304               || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10305             {
 10306               bool at_eob_p = false;
 10307 
 10308               if ((at_eob_p = !get_next_display_element (it))
 10309                   || BUFFER_POS_REACHED_P ()
 10310                   /* If we are past TO_CHARPOS, but never saw any
 10311                      character positions smaller than TO_CHARPOS,
 10312                      return MOVE_POS_MATCH_OR_ZV, like the
 10313                      unidirectional display did.  */
 10314                   || (it->bidi_p && (op & MOVE_TO_POS) != 0
 10315                       && !saw_smaller_pos
 10316                       && IT_CHARPOS (*it) > to_charpos))
 10317                 {
 10318                   if (it->bidi_p
 10319                       && !BUFFER_POS_REACHED_P ()
 10320                       && !at_eob_p && closest_pos < ZV)
 10321                     {
 10322                       RESTORE_IT (it, &ppos_it, ppos_data);
 10323                       if (closest_pos != to_charpos)
 10324                         move_it_in_display_line_to (it, closest_pos, -1,
 10325                                                     MOVE_TO_POS);
 10326                     }
 10327                   result = MOVE_POS_MATCH_OR_ZV;
 10328                   break;
 10329                 }
 10330               if (ITERATOR_AT_END_OF_LINE_P (it))
 10331                 {
 10332                   result = MOVE_NEWLINE_OR_CR;
 10333                   break;
 10334                 }
 10335             }
 10336           else if (it->bidi_p && (op & MOVE_TO_POS) != 0
 10337                    && !saw_smaller_pos
 10338                    && IT_CHARPOS (*it) > to_charpos)
 10339             {
 10340               if (closest_pos < ZV)
 10341                 {
 10342                   RESTORE_IT (it, &ppos_it, ppos_data);
 10343                   if (closest_pos != to_charpos)
 10344                     move_it_in_display_line_to (it, closest_pos, -1,
 10345                                                 MOVE_TO_POS);
 10346                 }
 10347               result = MOVE_POS_MATCH_OR_ZV;
 10348               break;
 10349             }
 10350           result = MOVE_LINE_TRUNCATED;
 10351           break;
 10352         }
 10353 #undef IT_RESET_X_ASCENT_DESCENT
 10354     }
 10355 
 10356 #undef BUFFER_POS_REACHED_P
 10357 
 10358   /* If we scanned beyond TO_POS, restore the saved iterator either to
 10359      the wrap point (if found), or to atpos/atx location.  We decide which
 10360      data to use to restore the saved iterator state by their X coordinates,
 10361      since buffer positions might increase non-monotonically with screen
 10362      coordinates due to bidi reordering.  */
 10363   if (result == MOVE_LINE_CONTINUED
 10364       && it->line_wrap == WORD_WRAP
 10365       && wrap_it.sp >= 0
 10366       && ((atpos_it.sp >= 0 && wrap_it.current_x < atpos_it.current_x)
 10367           || (atx_it.sp >= 0 && wrap_it.current_x < atx_it.current_x)))
 10368     RESTORE_IT (it, &wrap_it, wrap_data);
 10369   else if (atpos_it.sp >= 0)
 10370     RESTORE_IT (it, &atpos_it, atpos_data);
 10371   else if (atx_it.sp >= 0)
 10372     RESTORE_IT (it, &atx_it, atx_data);
 10373 
 10374  done:
 10375 
 10376   if (atpos_data)
 10377     bidi_unshelve_cache (atpos_data, true);
 10378   if (atx_data)
 10379     bidi_unshelve_cache (atx_data, true);
 10380   if (wrap_data)
 10381     bidi_unshelve_cache (wrap_data, true);
 10382   if (ppos_data)
 10383     bidi_unshelve_cache (ppos_data, true);
 10384 
 10385   /* Restore the iterator settings altered at the beginning of this
 10386      function.  */
 10387   it->glyph_row = saved_glyph_row;
 10388   return result;
 10389 }
 10390 
 10391 /* For external use.  */
 10392 void
 10393 move_it_in_display_line (struct it *it,
 10394                          ptrdiff_t to_charpos, int to_x,
 10395                          enum move_operation_enum op)
 10396 {
 10397   if (it->line_wrap == WORD_WRAP
 10398       && (op & MOVE_TO_X))
 10399     {
 10400       struct it save_it;
 10401       void *save_data = NULL;
 10402       int skip;
 10403 
 10404       SAVE_IT (save_it, *it, save_data);
 10405       skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10406       /* When word-wrap is on, TO_X may lie past the end
 10407          of a wrapped line.  Then it->current is the
 10408          character on the next line, so backtrack to the
 10409          space before the wrap point.  */
 10410       if (skip == MOVE_LINE_CONTINUED)
 10411         {
 10412           int prev_x = max (it->current_x - 1, 0);
 10413           RESTORE_IT (it, &save_it, save_data);
 10414           move_it_in_display_line_to
 10415             (it, -1, prev_x, MOVE_TO_X);
 10416         }
 10417       else
 10418         bidi_unshelve_cache (save_data, true);
 10419     }
 10420   else
 10421     move_it_in_display_line_to (it, to_charpos, to_x, op);
 10422 }
 10423 
 10424 
 10425 /* Move IT forward until it satisfies one or more of the criteria in
 10426    TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
 10427 
 10428    OP is a bit-mask that specifies where to stop, and in particular,
 10429    which of those four position arguments makes a difference.  See the
 10430    description of enum move_operation_enum.
 10431 
 10432    If TO_CHARPOS is in invisible text, e.g. a truncated part of a
 10433    screen line, this function will set IT to the next position that is
 10434    displayed to the right of TO_CHARPOS on the screen.
 10435 
 10436    Return the maximum pixel length of any line scanned but never more
 10437    than it.last_visible_x.  */
 10438 
 10439 int
 10440 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
 10441 {
 10442   enum move_it_result skip, skip2 = MOVE_X_REACHED;
 10443   int line_height, line_start_x = 0, reached = 0;
 10444   int max_current_x = 0;
 10445   void *backup_data = NULL;
 10446   ptrdiff_t orig_charpos = -1;
 10447   enum it_method orig_method = NUM_IT_METHODS;
 10448 
 10449   for (;;)
 10450     {
 10451       orig_charpos = IT_CHARPOS (*it);
 10452       orig_method = it->method;
 10453       if (op & MOVE_TO_VPOS)
 10454         {
 10455           /* If no TO_CHARPOS and no TO_X specified, stop at the
 10456              start of the line TO_VPOS.  */
 10457           if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
 10458             {
 10459               if (it->vpos == to_vpos)
 10460                 {
 10461                   reached = 1;
 10462                   break;
 10463                 }
 10464               else
 10465                 skip = move_it_in_display_line_to (it, -1, -1, 0);
 10466             }
 10467           else
 10468             {
 10469               /* TO_VPOS >= 0 means stop at TO_X in the line at
 10470                  TO_VPOS, or at TO_POS, whichever comes first.  */
 10471               if (it->vpos == to_vpos)
 10472                 {
 10473                   reached = 2;
 10474                   break;
 10475                 }
 10476 
 10477               skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
 10478 
 10479               if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
 10480                 {
 10481                   reached = 3;
 10482                   break;
 10483                 }
 10484               else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
 10485                 {
 10486                   /* We have reached TO_X but not in the line we want.  */
 10487                   skip = move_it_in_display_line_to (it, to_charpos,
 10488                                                      -1, MOVE_TO_POS);
 10489                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10490                     {
 10491                       reached = 4;
 10492                       break;
 10493                     }
 10494                 }
 10495             }
 10496         }
 10497       else if (op & MOVE_TO_Y)
 10498         {
 10499           struct it it_backup;
 10500 
 10501           if (it->line_wrap == WORD_WRAP)
 10502             SAVE_IT (it_backup, *it, backup_data);
 10503 
 10504           /* TO_Y specified means stop at TO_X in the line containing
 10505              TO_Y---or at TO_CHARPOS if this is reached first.  The
 10506              problem is that we can't really tell whether the line
 10507              contains TO_Y before we have completely scanned it, and
 10508              this may skip past TO_X.  What we do is to first scan to
 10509              TO_X.
 10510 
 10511              If TO_X is not specified, use a TO_X of zero.  The reason
 10512              is to make the outcome of this function more predictable.
 10513              If we didn't use TO_X == 0, we would stop at the end of
 10514              the line which is probably not what a caller would expect
 10515              to happen.  */
 10516           skip = move_it_in_display_line_to
 10517             (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
 10518              (MOVE_TO_X | (op & MOVE_TO_POS)));
 10519 
 10520           /* If TO_CHARPOS is reached or ZV, we don't have to do more.  */
 10521           if (skip == MOVE_POS_MATCH_OR_ZV)
 10522             reached = 5;
 10523           else if (skip == MOVE_X_REACHED)
 10524             {
 10525               /* If TO_X was reached, we want to know whether TO_Y is
 10526                  in the line.  We know this is the case if the already
 10527                  scanned glyphs make the line tall enough.  Otherwise,
 10528                  we must check by scanning the rest of the line.  */
 10529               line_height = it->max_ascent + it->max_descent;
 10530               if (to_y >= it->current_y
 10531                   && to_y < it->current_y + line_height)
 10532                 {
 10533                   reached = 6;
 10534                   break;
 10535                 }
 10536               SAVE_IT (it_backup, *it, backup_data);
 10537               move_trace ("move_it: from %td\n", IT_CHARPOS (*it));
 10538               skip2 = move_it_in_display_line_to (it, to_charpos, -1,
 10539                                                   op & MOVE_TO_POS);
 10540               move_trace ("move_it: to %td\n", IT_CHARPOS (*it));
 10541               line_height = it->max_ascent + it->max_descent;
 10542               move_trace ("move_it: line_height = %d\n", line_height);
 10543 
 10544               if (to_y >= it->current_y
 10545                   && to_y < it->current_y + line_height)
 10546                 {
 10547                   /* If TO_Y is in this line and TO_X was reached
 10548                      above, we scanned too far.  We have to restore
 10549                      IT's settings to the ones before skipping.  But
 10550                      keep the more accurate values of max_ascent and
 10551                      max_descent we've found while skipping the rest
 10552                      of the line, for the sake of callers, such as
 10553                      pos_visible_p, that need to know the line
 10554                      height.  */
 10555                   int max_ascent = it->max_ascent;
 10556                   int max_descent = it->max_descent;
 10557 
 10558                   RESTORE_IT (it, &it_backup, backup_data);
 10559                   it->max_ascent = max_ascent;
 10560                   it->max_descent = max_descent;
 10561                   reached = 6;
 10562                 }
 10563               else
 10564                 {
 10565                   skip = skip2;
 10566                   if (skip == MOVE_POS_MATCH_OR_ZV)
 10567                     {
 10568                       reached = 7;
 10569                       /* If the last move_it_in_display_line_to call
 10570                          took us away from TO_CHARPOS, back up to the
 10571                          previous position, as it is a better
 10572                          approximation of TO_CHARPOS.  (Note that we
 10573                          could have both positions after TO_CHARPOS or
 10574                          both positions before it, due to bidi
 10575                          reordering.)  */
 10576                       if (to_charpos > 0
 10577                           && IT_CHARPOS (*it) != to_charpos
 10578                           && ((IT_CHARPOS (it_backup) > to_charpos)
 10579                               == (IT_CHARPOS (*it) > to_charpos)))
 10580                         {
 10581                           int max_ascent = it->max_ascent;
 10582                           int max_descent = it->max_descent;
 10583 
 10584                           RESTORE_IT (it, &it_backup, backup_data);
 10585                           it->max_ascent = max_ascent;
 10586                           it->max_descent = max_descent;
 10587                         }
 10588                     }
 10589                 }
 10590             }
 10591           else
 10592             {
 10593               /* Check whether TO_Y is in this line.  */
 10594               line_height = it->max_ascent + it->max_descent;
 10595               move_trace ("move_it: line_height = %d\n", line_height);
 10596 
 10597               if (to_y >= it->current_y
 10598                   && to_y < it->current_y + line_height)
 10599                 {
 10600                   if (to_y > it->current_y)
 10601                     max_current_x = max (it->current_x, max_current_x);
 10602 
 10603                   /* When word-wrap is on, TO_X may lie past the end
 10604                      of a wrapped line.  Then it->current is the
 10605                      character on the next line, so backtrack to the
 10606                      space before the wrap point.  */
 10607                   if (skip == MOVE_LINE_CONTINUED
 10608                       && it->line_wrap == WORD_WRAP)
 10609                     {
 10610                       int prev_x = max (it->current_x - 1, 0);
 10611                       RESTORE_IT (it, &it_backup, backup_data);
 10612                       skip = move_it_in_display_line_to
 10613                         (it, -1, prev_x, MOVE_TO_X);
 10614                     }
 10615 
 10616                   reached = 6;
 10617                 }
 10618             }
 10619 
 10620           if (reached)
 10621             {
 10622               max_current_x = max (it->current_x, max_current_x);
 10623               break;
 10624             }
 10625         }
 10626       else if (BUFFERP (it->object)
 10627                && (it->method == GET_FROM_BUFFER
 10628                    || it->method == GET_FROM_STRETCH)
 10629                && IT_CHARPOS (*it) >= to_charpos
 10630                /* Under bidi iteration, a call to set_iterator_to_next
 10631                   can scan far beyond to_charpos if the initial
 10632                   portion of the next line needs to be reordered.  In
 10633                   that case, give move_it_in_display_line_to another
 10634                   chance below.  */
 10635                && !(it->bidi_p
 10636                     && it->bidi_it.scan_dir == -1))
 10637         skip = MOVE_POS_MATCH_OR_ZV;
 10638       else
 10639         skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
 10640 
 10641       switch (skip)
 10642         {
 10643         case MOVE_POS_MATCH_OR_ZV:
 10644           max_current_x = max (it->current_x, max_current_x);
 10645           reached = 8;
 10646           goto out;
 10647 
 10648         case MOVE_NEWLINE_OR_CR:
 10649           max_current_x = max (it->current_x, max_current_x);
 10650           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 10651             it->override_ascent = -1;
 10652           set_iterator_to_next (it, true);
 10653           it->continuation_lines_width = 0;
 10654           break;
 10655 
 10656         case MOVE_LINE_TRUNCATED:
 10657           max_current_x = it->last_visible_x;
 10658           it->continuation_lines_width = 0;
 10659           reseat_at_next_visible_line_start (it, false);
 10660           if ((op & MOVE_TO_POS) != 0
 10661               && (IT_CHARPOS (*it) > to_charpos
 10662                   || (IT_CHARPOS (*it) == to_charpos
 10663                       /* Consider TO_CHARPOS as REACHED if we are at
 10664                          EOB that ends in something other than a newline.  */
 10665                       && to_charpos == ZV
 10666                       && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n')
 10667                       /* But if we have a display or an overlay string
 10668                          at EOB, keep going until we exhaust all the
 10669                          characters of the string(s).  */
 10670                       && (it->sp == 0
 10671                           || (STRINGP (it->string)
 10672                               && (it->current.overlay_string_index < 0
 10673                                   || (it->current.overlay_string_index >= 0
 10674                                       && it->current.overlay_string_index
 10675                                          >= it->n_overlay_strings - 1))
 10676                               && IT_STRING_CHARPOS (*it) >= it->end_charpos)))))
 10677             {
 10678               reached = 9;
 10679               goto out;
 10680             }
 10681           break;
 10682 
 10683         case MOVE_LINE_CONTINUED:
 10684           max_current_x = it->last_visible_x;
 10685           /* For continued lines ending in a tab, some of the glyphs
 10686              associated with the tab are displayed on the current
 10687              line.  Since it->current_x does not include these glyphs,
 10688              we use it->last_visible_x instead.  */
 10689           if (it->c == '\t')
 10690             {
 10691               it->continuation_lines_width += it->last_visible_x;
 10692               /* When moving by vpos, ensure that the iterator really
 10693                  advances to the next line (bug#847, bug#969).  Fixme:
 10694                  do we need to do this in other circumstances?  */
 10695               if (it->current_x != it->last_visible_x
 10696                   && (op & MOVE_TO_VPOS)
 10697                   && !(op & (MOVE_TO_X | MOVE_TO_POS)))
 10698                 {
 10699                   line_start_x = it->current_x + it->pixel_width
 10700                     - it->last_visible_x;
 10701                   if (FRAME_WINDOW_P (it->f))
 10702                     {
 10703                       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 10704                       struct font *face_font = face->font;
 10705 
 10706                       /* When display_line produces a continued line
 10707                          that ends in a TAB, it skips a tab stop that
 10708                          is closer than the font's space character
 10709                          width (see gui_produce_glyphs where it produces
 10710                          the stretch glyph which represents a TAB).
 10711                          We need to reproduce the same logic here.  */
 10712                       eassert (face_font);
 10713                       if (face_font)
 10714                         {
 10715                           if (line_start_x < face_font->space_width)
 10716                             line_start_x
 10717                               += it->tab_width * face_font->space_width;
 10718                         }
 10719                     }
 10720                   set_iterator_to_next (it, false);
 10721                 }
 10722             }
 10723           else
 10724             {
 10725               /* Make sure we do advance, otherwise we might infloop.
 10726                  This could happen when the first display element is
 10727                  wider than the window, or if we have a wrap-prefix
 10728                  that doesn't leave enough space after it to display
 10729                  even a single character.  We only do this for moving
 10730                  through buffer text, as with display/overlay strings
 10731                  we'd need to also compare it->object's, and this is
 10732                  unlikely to happen in that case anyway.  */
 10733               if (IT_CHARPOS (*it) == orig_charpos
 10734                   && it->method == orig_method
 10735                   && orig_method == GET_FROM_BUFFER)
 10736                 set_iterator_to_next (it, false);
 10737               it->continuation_lines_width += it->current_x;
 10738             }
 10739           break;
 10740 
 10741         default:
 10742           emacs_abort ();
 10743         }
 10744 
 10745       /* Reset/increment for the next run.  */
 10746       it->current_x = line_start_x;
 10747       line_start_x = 0;
 10748       it->hpos = 0;
 10749       it->line_number_produced_p = false;
 10750       it->current_y += it->max_ascent + it->max_descent;
 10751       ++it->vpos;
 10752       last_height = it->max_ascent + it->max_descent;
 10753       it->max_ascent = it->max_descent = 0;
 10754     }
 10755 
 10756  out:
 10757 
 10758   /* On text terminals, we may stop at the end of a line in the middle
 10759      of a multi-character glyph.  If the glyph itself is continued,
 10760      i.e. it is actually displayed on the next line, don't treat this
 10761      stopping point as valid; move to the next line instead (unless
 10762      that brings us offscreen).  */
 10763   if (!FRAME_WINDOW_P (it->f)
 10764       && op & MOVE_TO_POS
 10765       && IT_CHARPOS (*it) == to_charpos
 10766       && it->what == IT_CHARACTER
 10767       && it->nglyphs > 1
 10768       && it->line_wrap == WINDOW_WRAP
 10769       && it->current_x == it->last_visible_x - 1
 10770       && it->c != '\n'
 10771       && it->c != '\t'
 10772       && it->w->window_end_valid
 10773       && it->vpos < it->w->window_end_vpos)
 10774     {
 10775       it->continuation_lines_width += it->current_x;
 10776       it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
 10777       it->current_y += it->max_ascent + it->max_descent;
 10778       ++it->vpos;
 10779       last_height = it->max_ascent + it->max_descent;
 10780     }
 10781 
 10782   if (backup_data)
 10783     bidi_unshelve_cache (backup_data, true);
 10784 
 10785   move_trace ("move_it_to: reached %d\n", reached);
 10786 
 10787   return max_current_x;
 10788 }
 10789 
 10790 
 10791 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
 10792 
 10793    If DY > 0, move IT backward that many pixels.
 10794    DY = 0 means move IT backward to the preceding line start or to BEGV.
 10795    This function may move over less or more than DY pixels if
 10796    IT->current_y - DY ends up in the middle of a line; in this case
 10797    IT->current_y will be set to the top of the line either before or
 10798    after the exact pixel coordinate.  */
 10799 
 10800 void
 10801 move_it_vertically_backward (struct it *it, int dy)
 10802 {
 10803   int nlines, h;
 10804   struct it it2, it3;
 10805   void *it2data = NULL, *it3data = NULL;
 10806   ptrdiff_t start_pos;
 10807   int nchars_per_row
 10808     = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 10809   ptrdiff_t pos_limit;
 10810 
 10811  move_further_back:
 10812   eassert (dy >= 0);
 10813 
 10814   start_pos = IT_CHARPOS (*it);
 10815 
 10816   /* Estimate how many newlines we must move back.  */
 10817   nlines = max (1, dy / default_line_pixel_height (it->w));
 10818   if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 10819     pos_limit = BEGV;
 10820   else
 10821     pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
 10822 
 10823   /* Set the iterator's position that many lines back.  But don't go
 10824      back more than NLINES full screen lines -- this wins a day with
 10825      buffers which have very long lines.  */
 10826   while (nlines-- && IT_CHARPOS (*it) > pos_limit)
 10827     back_to_previous_visible_line_start (it);
 10828 
 10829   /* Reseat the iterator here.  When moving backward, we don't want
 10830      reseat to skip forward over invisible text, set up the iterator
 10831      to deliver from overlay strings at the new position etc.  So,
 10832      use reseat_1 here.  */
 10833   reseat_1 (it, it->current.pos, true);
 10834 
 10835   /* We are now surely at a line start.  */
 10836   it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
 10837                                    reordering is in effect.  */
 10838   it->continuation_lines_width = 0;
 10839 
 10840   /* Move forward and see what y-distance we moved.  First move to the
 10841      start of the next line so that we get its height.  We need this
 10842      height to be able to tell whether we reached the specified
 10843      y-distance.  */
 10844   SAVE_IT (it2, *it, it2data);
 10845   it2.max_ascent = it2.max_descent = 0;
 10846   do
 10847     {
 10848       move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
 10849                   MOVE_TO_POS | MOVE_TO_VPOS);
 10850     }
 10851   while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
 10852            /* If we are in a display string which starts at START_POS,
 10853               and that display string includes a newline, and we are
 10854               right after that newline (i.e. at the beginning of a
 10855               display line), exit the loop, because otherwise we will
 10856               infloop, since move_it_to will see that it is already at
 10857               START_POS and will not move.  */
 10858            || (it2.method == GET_FROM_STRING
 10859                && IT_CHARPOS (it2) == start_pos
 10860                && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
 10861   eassert (IT_CHARPOS (*it) >= BEGV);
 10862   SAVE_IT (it3, it2, it3data);
 10863 
 10864   move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
 10865   eassert (IT_CHARPOS (*it) >= BEGV);
 10866   /* H is the actual vertical distance from the position in *IT
 10867      and the starting position.  */
 10868   h = it2.current_y - it->current_y;
 10869   /* NLINES is the distance in number of lines.  */
 10870   nlines = it2.vpos - it->vpos;
 10871 
 10872   /* Correct IT's y and vpos position
 10873      so that they are relative to the starting point.  */
 10874   it->vpos -= nlines;
 10875   it->current_y -= h;
 10876 
 10877   if (dy == 0)
 10878     {
 10879       /* DY == 0 means move to the start of the screen line.  The
 10880          value of nlines is > 0 if continuation lines were involved,
 10881          or if the original IT position was at start of a line.  */
 10882       RESTORE_IT (it, it, it2data);
 10883       if (nlines > 0)
 10884         move_it_by_lines (it, nlines);
 10885       /* The above code moves us to some position NLINES down,
 10886          usually to its first glyph (leftmost in an L2R line), but
 10887          that's not necessarily the start of the line, under bidi
 10888          reordering.  We want to get to the character position
 10889          that is immediately after the newline of the previous
 10890          line.  */
 10891       if (it->bidi_p
 10892           && !it->continuation_lines_width
 10893           && !STRINGP (it->string)
 10894           && IT_CHARPOS (*it) > BEGV
 10895           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10896         {
 10897           ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
 10898 
 10899           dec_both (&cp, &bp);
 10900           SET_WITH_NARROWED_BEGV (it, cp,
 10901                                   find_newline_no_quit (cp, bp, -1, NULL),
 10902                                   get_small_narrowing_begv (it->w, IT_CHARPOS (*it)));
 10903           move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
 10904         }
 10905       bidi_unshelve_cache (it3data, true);
 10906     }
 10907   else
 10908     {
 10909       /* The y-position we try to reach, relative to *IT.
 10910          Note that H has been subtracted in front of the if-statement.  */
 10911       int target_y = it->current_y + h - dy;
 10912       int y0 = it3.current_y;
 10913       int y1;
 10914       int line_height;
 10915 
 10916       RESTORE_IT (&it3, &it3, it3data);
 10917       y1 = line_bottom_y (&it3);
 10918       line_height = y1 - y0;
 10919       RESTORE_IT (it, it, it2data);
 10920       /* If we did not reach target_y, try to move further backward if
 10921          we can.  If we moved too far backward, try to move forward.  */
 10922       if (target_y < it->current_y
 10923           /* This is heuristic.  In a window that's 3 lines high, with
 10924              a line height of 13 pixels each, recentering with point
 10925              on the bottom line will try to move -39/2 = 19 pixels
 10926              backward.  Try to avoid moving into the first line.  */
 10927           && (it->current_y - target_y
 10928               > min (window_box_height (it->w), line_height * 2 / 3))
 10929           && IT_CHARPOS (*it) > BEGV)
 10930         {
 10931           move_trace ("  not far enough -> move_vert %d\n",
 10932                       target_y - it->current_y);
 10933           dy = it->current_y - target_y;
 10934           goto move_further_back;
 10935         }
 10936       else if (target_y >= it->current_y + line_height
 10937                && IT_CHARPOS (*it) < ZV)
 10938         {
 10939           /* Should move forward by at least one line, maybe more.
 10940 
 10941              Note: Calling move_it_by_lines can be expensive on
 10942              terminal frames, where compute_motion is used (via
 10943              vmotion) to do the job, when there are very long lines
 10944              and truncate-lines is nil.  That's the reason for
 10945              treating terminal frames specially here.  */
 10946 
 10947           if (!FRAME_WINDOW_P (it->f))
 10948             move_it_vertically (it, target_y - it->current_y);
 10949           else
 10950             {
 10951               struct text_pos last_pos;
 10952               int last_y, last_vpos;
 10953               do
 10954                 {
 10955                   last_pos = it->current.pos;
 10956                   last_y = it->current_y;
 10957                   last_vpos = it->vpos;
 10958                   move_it_by_lines (it, 1);
 10959                 }
 10960               while (target_y > it->current_y && IT_CHARPOS (*it) < ZV);
 10961               if (it->current_y > target_y)
 10962                 {
 10963                   reseat (it, last_pos, true);
 10964                   it->current_y = last_y;
 10965                   it->vpos = last_vpos;
 10966                 }
 10967             }
 10968         }
 10969     }
 10970 }
 10971 
 10972 
 10973 /* Move IT by a specified amount of pixel lines DY.  DY negative means
 10974    move backwards.  DY = 0 means move to start of screen line.  At the
 10975    end, IT will be on the start of a screen line.  */
 10976 
 10977 void
 10978 move_it_vertically (struct it *it, int dy)
 10979 {
 10980   if (dy <= 0)
 10981     move_it_vertically_backward (it, -dy);
 10982   else
 10983     {
 10984       move_trace ("move_it_v: from %td, %d\n", IT_CHARPOS (*it), dy);
 10985       move_it_to (it, ZV, -1, it->current_y + dy, -1,
 10986                   MOVE_TO_POS | MOVE_TO_Y);
 10987       move_trace ("move_it_v: to %td\n", IT_CHARPOS (*it));
 10988 
 10989       /* If buffer ends in ZV without a newline, move to the start of
 10990          the line to satisfy the post-condition.  */
 10991       if (IT_CHARPOS (*it) == ZV
 10992           && ZV > BEGV
 10993           && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
 10994         move_it_by_lines (it, 0);
 10995     }
 10996 }
 10997 
 10998 
 10999 /* Move iterator IT past the end of the text line it is in.  */
 11000 
 11001 void
 11002 move_it_past_eol (struct it *it)
 11003 {
 11004   enum move_it_result rc;
 11005 
 11006   rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
 11007   if (rc == MOVE_NEWLINE_OR_CR)
 11008     set_iterator_to_next (it, false);
 11009 }
 11010 
 11011 
 11012 /* Move IT by a specified number DVPOS of screen lines down.  DVPOS
 11013    negative means move up.  DVPOS == 0 means move to the start of the
 11014    screen line.
 11015 
 11016    Optimization idea: If we would know that IT->f doesn't use
 11017    a face with proportional font, we could be faster for
 11018    truncate-lines nil.  */
 11019 
 11020 void
 11021 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
 11022 {
 11023 
 11024   /* The commented-out optimization uses vmotion on terminals.  This
 11025      gives bad results, because elements like it->what, on which
 11026      callers such as pos_visible_p rely, aren't updated.  */
 11027   /* struct position pos;
 11028     if (!FRAME_WINDOW_P (it->f))
 11029     {
 11030       struct text_pos textpos;
 11031 
 11032       pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
 11033       SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
 11034       reseat (it, textpos, true);
 11035       it->vpos += pos.vpos;
 11036       it->current_y += pos.vpos;
 11037     }
 11038     else */
 11039 
 11040   if (dvpos == 0)
 11041     {
 11042       /* DVPOS == 0 means move to the start of the screen line.  */
 11043       move_it_vertically_backward (it, 0);
 11044       /* Let next call to line_bottom_y calculate real line height.  */
 11045       last_height = 0;
 11046     }
 11047   else if (dvpos > 0)
 11048     {
 11049       move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
 11050       if (!IT_POS_VALID_AFTER_MOVE_P (it))
 11051         {
 11052           /* Only move to the next buffer position if we ended up in a
 11053              string from display property, not in an overlay string
 11054              (before-string or after-string).  That is because the
 11055              latter don't conceal the underlying buffer position, so
 11056              we can ask to move the iterator to the exact position we
 11057              are interested in.  Note that, even if we are already at
 11058              IT_CHARPOS (*it), the call below is not a no-op, as it
 11059              will detect that we are at the end of the string, pop the
 11060              iterator, and compute it->current_x and it->hpos
 11061              correctly.  */
 11062           move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
 11063                       -1, -1, -1, MOVE_TO_POS);
 11064         }
 11065     }
 11066   else
 11067     {
 11068       struct it it2;
 11069       void *it2data = NULL;
 11070       ptrdiff_t start_charpos, orig_charpos, i;
 11071       int nchars_per_row
 11072         = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
 11073       bool hit_pos_limit = false;
 11074       ptrdiff_t pos_limit;
 11075 
 11076       /* Start at the beginning of the screen line containing IT's
 11077          position.  This may actually move vertically backwards,
 11078          in case of overlays, so adjust dvpos accordingly.  */
 11079       dvpos += it->vpos;
 11080       orig_charpos = IT_CHARPOS (*it);
 11081       move_it_vertically_backward (it, 0);
 11082       dvpos -= it->vpos;
 11083 
 11084       /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
 11085          screen lines, and reseat the iterator there.  */
 11086       start_charpos = IT_CHARPOS (*it);
 11087       if (it->line_wrap == TRUNCATE || nchars_per_row == 0)
 11088         pos_limit = BEGV;
 11089       else
 11090         pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
 11091 
 11092       for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
 11093         back_to_previous_visible_line_start (it);
 11094       if (i > 0 && IT_CHARPOS (*it) <= pos_limit)
 11095         hit_pos_limit = true;
 11096       reseat (it, it->current.pos, true);
 11097 
 11098       /* Move further back if we end up in a string or an image.  */
 11099       while (!IT_POS_VALID_AFTER_MOVE_P (it))
 11100         {
 11101           /* First try to move to start of display line.  */
 11102           dvpos += it->vpos;
 11103           move_it_vertically_backward (it, 0);
 11104           dvpos -= it->vpos;
 11105           if (IT_POS_VALID_AFTER_MOVE_P (it))
 11106             break;
 11107           /* If start of line is still in string or image,
 11108              move further back.  */
 11109           back_to_previous_visible_line_start (it);
 11110           reseat (it, it->current.pos, true);
 11111           dvpos--;
 11112         }
 11113 
 11114       it->current_x = it->hpos = 0;
 11115 
 11116       /* Above call may have moved too far if continuation lines
 11117          are involved.  Scan forward and see if it did.  */
 11118       SAVE_IT (it2, *it, it2data);
 11119       it2.vpos = it2.current_y = 0;
 11120       move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
 11121       it->vpos -= it2.vpos;
 11122       it->current_y -= it2.current_y;
 11123       it->current_x = it->hpos = 0;
 11124 
 11125       /* If we moved too far back, move IT some lines forward.  */
 11126       if (it2.vpos > -dvpos)
 11127         {
 11128           int delta = it2.vpos + dvpos;
 11129 
 11130           RESTORE_IT (&it2, &it2, it2data);
 11131           SAVE_IT (it2, *it, it2data);
 11132           move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
 11133           /* Move back again if we got too far ahead,
 11134              or didn't move at all.  */
 11135           if (it->vpos - it2.vpos > delta || IT_CHARPOS (*it) == orig_charpos)
 11136             RESTORE_IT (it, &it2, it2data);
 11137           else
 11138             bidi_unshelve_cache (it2data, true);
 11139         }
 11140       else if (hit_pos_limit && pos_limit > BEGV
 11141                && dvpos < 0 && it2.vpos < -dvpos)
 11142         {
 11143           /* If we hit the limit, but still didn't make it far enough
 11144              back, that means there's a display string with a newline
 11145              covering a large chunk of text, and that caused
 11146              back_to_previous_visible_line_start try to go too far.
 11147              Punish those who commit such atrocities by going back
 11148              until we've reached DVPOS, after lifting the limit, which
 11149              could make it slow for very long lines.  "If it hurts,
 11150              don't do that!"  */
 11151           dvpos += it2.vpos;
 11152           RESTORE_IT (it, it, it2data);
 11153           for (i = -dvpos; i > 0; --i)
 11154             {
 11155               back_to_previous_visible_line_start (it);
 11156               it->vpos--;
 11157             }
 11158           reseat_1 (it, it->current.pos, true);
 11159         }
 11160       else
 11161         RESTORE_IT (it, it, it2data);
 11162     }
 11163 }
 11164 
 11165 int
 11166 partial_line_height (struct it *it_origin)
 11167 {
 11168   /* In a buffer with very long and truncated lines, we ignore the
 11169      possibly-partial height of the last line in the window: it is too
 11170      expensive to compute that (since in most cases that involves
 11171      going all the way to ZV), and the effect of ignoring it is
 11172      relatively minor.  */
 11173   if (XBUFFER (it_origin->w->contents)->long_line_optimizations_p
 11174       && it_origin->line_wrap == TRUNCATE)
 11175     return 0;
 11176 
 11177   int partial_height;
 11178   void *it_data = NULL;
 11179   struct it it;
 11180   SAVE_IT (it, *it_origin, it_data);
 11181   move_it_to (&it, ZV, -1, it.last_visible_y, -1,
 11182               MOVE_TO_POS | MOVE_TO_Y);
 11183   if (it.what == IT_EOB)
 11184     {
 11185       int vis_height = it.last_visible_y - it.current_y;
 11186       int height = it.ascent + it.descent;
 11187       partial_height = (vis_height < height) ? vis_height : 0;
 11188     }
 11189   else
 11190     {
 11191       int last_line_y = it.current_y;
 11192       move_it_by_lines (&it, 1);
 11193       partial_height = (it.current_y > it.last_visible_y)
 11194         ? it.last_visible_y - last_line_y : 0;
 11195     }
 11196   RESTORE_IT (&it, &it, it_data);
 11197   return partial_height;
 11198 }
 11199 
 11200 /* Approximate move_it_in_display_line_to for very long and truncated
 11201    display lines, when moving horizontally.  This is used when the
 11202    buffer's long_line_optimizations_p flag is set.  It ignores various
 11203    complications, like different font sizes, invisible text, display
 11204    and overlay strings, and, to some degree, bidirectional text.  So
 11205    caveat emptor!
 11206 
 11207    Starting from IT's position, reseat IT after skipping NCHARS
 11208    characters or to the next newline/ZV, whichever comes first.  Return
 11209    what move_it_in_display_line_to would have returned in this case.  */
 11210 
 11211 static enum move_it_result
 11212 fast_move_it_horizontally (struct it *it, ptrdiff_t nchars)
 11213 {
 11214   ptrdiff_t nl_bytepos;
 11215   ptrdiff_t nl_pos = find_newline_no_quit (IT_CHARPOS (*it), IT_BYTEPOS (*it),
 11216                                            1, &nl_bytepos);
 11217   struct text_pos new_pos;
 11218   enum move_it_result move_result;
 11219 
 11220   if (nl_pos - IT_CHARPOS (*it) > nchars)
 11221     {
 11222       SET_TEXT_POS (new_pos,
 11223                     IT_CHARPOS (*it) + nchars,
 11224                     CHAR_TO_BYTE (IT_CHARPOS (*it) + nchars));
 11225       move_result = MOVE_X_REACHED;
 11226     }
 11227   else
 11228     {
 11229       if (nl_bytepos < ZV_BYTE
 11230           || (nl_bytepos > BEGV_BYTE
 11231               && FETCH_BYTE (nl_bytepos - 1) == '\n'))
 11232         {
 11233           nl_pos--;
 11234           nl_bytepos--;
 11235           move_result = MOVE_NEWLINE_OR_CR;
 11236         }
 11237       else
 11238         move_result = MOVE_POS_MATCH_OR_ZV;
 11239       SET_TEXT_POS (new_pos, nl_pos, nl_bytepos);
 11240     }
 11241   reseat (it, new_pos, false);
 11242   return move_result;
 11243 }
 11244 
 11245 /* Return true if IT points into the middle of a display vector.  */
 11246 
 11247 bool
 11248 in_display_vector_p (struct it *it)
 11249 {
 11250   return (it->method == GET_FROM_DISPLAY_VECTOR
 11251           && it->current.dpvec_index > 0
 11252           && it->dpvec + it->current.dpvec_index != it->dpend);
 11253 }
 11254 
 11255 /* This is like Fwindow_text_pixel_size but assumes that WINDOW's buffer
 11256    is the current buffer.  Fbuffer_text_pixel_size calls it after it has
 11257    set WINDOW's buffer to the buffer specified by its BUFFER_OR_NAME
 11258    argument.  */
 11259 static Lisp_Object
 11260 window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 11261                         Lisp_Object x_limit, Lisp_Object y_limit,
 11262                         Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11263 {
 11264   struct window *w = decode_live_window (window);
 11265   struct it it;
 11266   ptrdiff_t start, end, bpos;
 11267   struct text_pos startp;
 11268   void *itdata = NULL;
 11269   int c, max_x = 0, max_y = 0, x = 0, y = 0, vertical_offset = 0, doff = 0;
 11270 
 11271   if (NILP (from))
 11272     {
 11273       start = BEGV;
 11274       bpos = BEGV_BYTE;
 11275     }
 11276   else if (EQ (from, Qt))
 11277     {
 11278       start = BEGV;
 11279       bpos = BEGV_BYTE;
 11280       while (bpos < ZV_BYTE)
 11281         {
 11282           c = FETCH_BYTE (bpos);
 11283           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11284             break;
 11285           inc_both (&start, &bpos);
 11286         }
 11287       while (bpos > BEGV_BYTE)
 11288         {
 11289           dec_both (&start, &bpos);
 11290           c = FETCH_BYTE (bpos);
 11291           if (!(c == ' ' || c == '\t'))
 11292             break;
 11293         }
 11294     }
 11295   else if (CONSP (from))
 11296     {
 11297       start = clip_to_bounds (BEGV, fix_position (XCAR (from)), ZV);
 11298       bpos = CHAR_TO_BYTE (start);
 11299       CHECK_FIXNUM (XCDR (from));
 11300       vertical_offset = XFIXNUM (XCDR (from));
 11301     }
 11302   else
 11303     {
 11304       start = clip_to_bounds (BEGV, fix_position (from), ZV);
 11305       bpos = CHAR_TO_BYTE (start);
 11306     }
 11307 
 11308   SET_TEXT_POS (startp, start, bpos);
 11309 
 11310   if (NILP (to))
 11311     end = ZV;
 11312   else if (EQ (to, Qt))
 11313     {
 11314       end = ZV;
 11315       bpos = ZV_BYTE;
 11316       while (bpos > BEGV_BYTE)
 11317         {
 11318           dec_both (&end, &bpos);
 11319           c = FETCH_BYTE (bpos);
 11320           if (!(c == ' ' || c == '\t' || c == '\n' || c == '\r'))
 11321             {
 11322               inc_both (&end, &bpos);
 11323               break;
 11324             }
 11325         }
 11326       while (bpos < ZV_BYTE)
 11327         {
 11328           c = fetch_char_advance (&end, &bpos);
 11329           if (!(c == ' ' || c == '\t'))
 11330             break;
 11331         }
 11332     }
 11333   else
 11334     end = clip_to_bounds (start, fix_position (to), ZV);
 11335 
 11336   if (RANGED_FIXNUMP (0, x_limit, INT_MAX))
 11337     max_x = XFIXNUM (x_limit);
 11338   else if (!NILP (x_limit))
 11339     max_x = INT_MAX;
 11340 
 11341   if (NILP (y_limit))
 11342     max_y = INT_MAX;
 11343   else if (RANGED_FIXNUMP (0, y_limit, INT_MAX))
 11344     max_y = XFIXNUM (y_limit);
 11345 
 11346   itdata = bidi_shelve_cache ();
 11347 
 11348   start_display (&it, w, startp);
 11349 
 11350   int start_y = it.current_y;
 11351 
 11352   /* It makes no sense to measure dimensions of region of text that
 11353      crosses the point where bidi reordering changes scan direction.
 11354      By using unidirectional movement here we at least support the use
 11355      case of measuring regions of text that have a uniformly R2L
 11356      directionality, and regions that begin and end in text of the
 11357      same directionality.  */
 11358   it.bidi_p = false;
 11359 
 11360   int start_x;
 11361   if (vertical_offset != 0)
 11362     {
 11363       int last_y;
 11364       it.current_y = 0;
 11365 
 11366       move_it_by_lines (&it, 0);
 11367 
 11368       /* `move_it_vertically_backward' is called by move_it_vertically
 11369          to move by a negative value (upwards), but it is not always
 11370          guaranteed to leave the iterator at or above the position
 11371          given by the offset, which this loop ensures.  */
 11372       if (vertical_offset < 0)
 11373         {
 11374           while (it.current_y > vertical_offset)
 11375             {
 11376               last_y = it.current_y;
 11377               move_it_vertically_backward (&it,
 11378                                            (abs (vertical_offset)
 11379                                             + it.current_y));
 11380 
 11381               if (it.current_y == last_y)
 11382                 break;
 11383             }
 11384         }
 11385       else
 11386         {
 11387           move_it_vertically (&it, vertical_offset);
 11388         }
 11389 
 11390       it.current_y = (WINDOW_TAB_LINE_HEIGHT (w)
 11391                       + WINDOW_HEADER_LINE_HEIGHT (w));
 11392       start = clip_to_bounds (BEGV, IT_CHARPOS (it), ZV);
 11393       start_y = it.current_y;
 11394       start_x = it.current_x;
 11395     }
 11396   else
 11397     {
 11398       /* Start at the beginning of the line containing FROM.  Otherwise
 11399          IT.current_x will be incorrectly set to zero at some arbitrary
 11400          non-zero X coordinate.  */
 11401       reseat_at_previous_visible_line_start (&it);
 11402       it.current_x = it.hpos = 0;
 11403       if (IT_CHARPOS (it) != start)
 11404         {
 11405           void *it1data = NULL;
 11406           struct it it1;
 11407 
 11408           SAVE_IT (it1, it, it1data);
 11409           move_it_to (&it, start, -1, -1, -1, MOVE_TO_POS);
 11410           /* We could have a display property at START, in which case
 11411              asking move_it_to to stop at START will overshoot and
 11412              stop at position after START.  So we try again, stopping
 11413              before START, and account for the width of the last
 11414              buffer position manually.  */
 11415           if (IT_CHARPOS (it) > start && start > BEGV)
 11416             {
 11417               ptrdiff_t it1pos = IT_CHARPOS (it1);
 11418               int it1_x = it1.current_x;
 11419 
 11420               RESTORE_IT (&it, &it1, it1data);
 11421               /* If START - 1 is the beginning of screen line,
 11422                  move_it_to will not move, so we need to use a
 11423                  lower-level move_it_in_display_line subroutine, and
 11424                  tell it to move just 1 pixel, so it stops at the next
 11425                  display element.  */
 11426               if (start - 1 > it1pos)
 11427                 move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11428               else
 11429                 move_it_in_display_line (&it, start, it1_x + 1,
 11430                                          MOVE_TO_POS | MOVE_TO_X);
 11431               move_it_to (&it, start - 1, -1, -1, -1, MOVE_TO_POS);
 11432               start_x = it.current_x;
 11433               /* If we didn't change our buffer position, the pixel
 11434                  width of what's here was not yet accounted for; do it
 11435                  manually.  */
 11436               if (IT_CHARPOS (it) == start - 1)
 11437                 start_x += it.pixel_width;
 11438             }
 11439           else
 11440             {
 11441               start_x = it.current_x;
 11442               bidi_unshelve_cache (it1data, true);
 11443             }
 11444         }
 11445       else
 11446         start_x = it.current_x;
 11447     }
 11448 
 11449   /* Now move to TO.  */
 11450   int move_op = MOVE_TO_POS | MOVE_TO_Y;
 11451   int to_x = -1;
 11452   it.current_y = start_y;
 11453   /* If FROM is on a newline, pretend that we start at the beginning
 11454      of the next line, because the newline takes no place on display.  */
 11455   if (FETCH_BYTE (start) == '\n')
 11456     it.current_x = 0;
 11457   if (!NILP (x_limit))
 11458     {
 11459       it.last_visible_x = max_x;
 11460       /* Actually, we never want move_it_to stop at to_x.  But to make
 11461          sure that move_it_in_display_line_to always moves far enough,
 11462          we set to_x to INT_MAX and specify MOVE_TO_X.  */
 11463       move_op |= MOVE_TO_X;
 11464       to_x = INT_MAX;
 11465     }
 11466 
 11467   void *it2data = NULL;
 11468   struct it it2;
 11469   SAVE_IT (it2, it, it2data);
 11470 
 11471   x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11472 
 11473   /* We could have a display property at END, in which case asking
 11474      move_it_to to stop at END will overshoot and stop at position
 11475      after END.  So we try again, stopping before END, and account for
 11476      the width of the last buffer position manually.  */
 11477   if (IT_CHARPOS (it) > end)
 11478     {
 11479       end--;
 11480       RESTORE_IT (&it, &it2, it2data);
 11481       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
 11482       /* Add the width of the thing at TO, but only if we didn't
 11483          overshoot it; if we did, it is already accounted for.  Also,
 11484          account for the height of the thing at TO.  */
 11485       if (IT_CHARPOS (it) == end)
 11486         {
 11487           x += it.pixel_width;
 11488 
 11489           /* DTRT if ignore_line_at_end is t.  */
 11490           if (!NILP (ignore_line_at_end))
 11491             doff = (max (it.max_ascent, it.ascent)
 11492                     + max (it.max_descent, it.descent));
 11493           else
 11494             {
 11495               it.max_ascent = max (it.max_ascent, it.ascent);
 11496               it.max_descent = max (it.max_descent, it.descent);
 11497             }
 11498         }
 11499     }
 11500   else
 11501     bidi_unshelve_cache (it2data, true);
 11502 
 11503   if (!NILP (x_limit))
 11504     {
 11505       /* Don't return more than X-LIMIT.  */
 11506       if (x > max_x)
 11507         x = max_x;
 11508     }
 11509 
 11510   /* If text spans more than one screen line, we don't need to adjust
 11511      the x-span for start_x, since the second and subsequent lines
 11512      will begin at zero X coordinate.  */
 11513   if (it.current_y > start_y)
 11514     start_x = 0;
 11515 
 11516   /* Subtract height of header-line and tab-line which was counted
 11517      automatically by start_display.  */
 11518   if (!NILP (ignore_line_at_end))
 11519     y = (it.current_y + doff
 11520          - WINDOW_TAB_LINE_HEIGHT (w)
 11521          - WINDOW_HEADER_LINE_HEIGHT (w));
 11522   else
 11523     y = (it.current_y + it.max_ascent + it.max_descent + doff
 11524          - WINDOW_TAB_LINE_HEIGHT (w) - WINDOW_HEADER_LINE_HEIGHT (w));
 11525 
 11526   /* Don't return more than Y-LIMIT.  */
 11527   if (y > max_y)
 11528     y = max_y;
 11529 
 11530   if ((EQ (mode_lines, Qtab_line) || EQ (mode_lines, Qt))
 11531       && window_wants_tab_line (w))
 11532     /* Add height of tab-line as requested.  */
 11533     {
 11534       Lisp_Object window_tab_line_format
 11535         = window_parameter (w, Qtab_line_format);
 11536 
 11537       y = y + display_mode_line (w, TAB_LINE_FACE_ID,
 11538                                  NILP (window_tab_line_format)
 11539                                  ? BVAR (current_buffer, tab_line_format)
 11540                                  : window_tab_line_format);
 11541     }
 11542 
 11543   if ((EQ (mode_lines, Qheader_line) || EQ (mode_lines, Qt))
 11544       && window_wants_header_line (w))
 11545     {
 11546       Lisp_Object window_header_line_format
 11547         = window_parameter (w, Qheader_line_format);
 11548 
 11549       y = y + display_mode_line (w, HEADER_LINE_FACE_ID,
 11550                                  NILP (window_header_line_format)
 11551                                  ? BVAR (current_buffer, header_line_format)
 11552                                  : window_header_line_format);
 11553     }
 11554 
 11555   if ((EQ (mode_lines, Qmode_line) || EQ (mode_lines, Qt))
 11556       && window_wants_mode_line (w))
 11557     {
 11558       Lisp_Object window_mode_line_format
 11559         = window_parameter (w, Qmode_line_format);
 11560 
 11561       y = y + display_mode_line (w, CURRENT_MODE_LINE_ACTIVE_FACE_ID (w),
 11562                                  NILP (window_mode_line_format)
 11563                                  ? BVAR (current_buffer, mode_line_format)
 11564                                  : window_mode_line_format);
 11565     }
 11566 
 11567   bidi_unshelve_cache (itdata, false);
 11568 
 11569   return (!vertical_offset
 11570           ? Fcons (make_fixnum (x - start_x), make_fixnum (y))
 11571           : list3i (x - start_x, y, start));
 11572 }
 11573 
 11574 DEFUN ("window-text-pixel-size", Fwindow_text_pixel_size, Swindow_text_pixel_size, 0, 7, 0,
 11575        doc: /* Return the size of the text of WINDOW's buffer in pixels.
 11576 WINDOW must be a live window and defaults to the selected one.  The
 11577 return value is a cons of the maximum pixel-width of any text line and
 11578 the pixel-height of all the text lines in the accessible portion of
 11579 buffer text.
 11580 
 11581 If FROM is a cons cell, the return value includes, in addition to the
 11582 dimensions, also a third element that provides the buffer position
 11583 from which measuring of the text dimensions was actually started.
 11584 
 11585 This function exists to allow Lisp programs to adjust the dimensions
 11586 of WINDOW to the buffer text it needs to display.
 11587 
 11588 The optional argument FROM, if non-nil, specifies the first text
 11589 position to consider, and defaults to the minimum accessible position
 11590 of the buffer.  If FROM is a cons, its car specifies a buffer
 11591 position, and its cdr specifies the vertical offset in pixels from
 11592 that position to the first screen line to be measured.  If FROM is t,
 11593 it stands for the minimum accessible position that starts a non-empty
 11594 line.  TO, if non-nil, specifies the last text position and defaults
 11595 to the maximum accessible position of the buffer.  If TO is t, it
 11596 stands for the maximum accessible position that ends a non-empty line.
 11597 
 11598 The optional argument X-LIMIT, if non-nil, specifies the maximum X
 11599 coordinate beyond which the text should be ignored.  It is therefore
 11600 also the maximum width that the function can return.  X-LIMIT nil or
 11601 omitted means to use the pixel-width of WINDOW's body.  This default
 11602 means text of truncated lines wider than the window will be ignored;
 11603 specify a non-nil value for X-LIMIT if lines are truncated and you need
 11604 to account for the truncated text.
 11605 
 11606 Use nil for X-LIMIT if you want to know how high WINDOW should become in
 11607 order to fit all of its buffer's text with the width of WINDOW
 11608 unaltered.  Use the maximum width WINDOW may assume if you intend to
 11609 change WINDOW's width.  Use t for the maximum possible value.  Since
 11610 calculating the width of long lines can take some time, it's always a
 11611 good idea to make this argument as small as possible; in particular, if
 11612 the buffer contains long lines that shall be truncated anyway.
 11613 
 11614 The optional argument Y-LIMIT, if non-nil, specifies the maximum Y
 11615 coordinate beyond which the text is to be ignored; it is therefore
 11616 also the maximum height that the function can return (excluding the
 11617 height of the mode- or header-line, if any).  Y-LIMIT nil or omitted
 11618 means consider all of the accessible portion of buffer text up to the
 11619 position specified by TO.  Since calculating the text height of a
 11620 large buffer can take some time, it makes sense to specify this
 11621 argument if the size of the buffer is large or unknown.
 11622 
 11623 Optional argument MODE-LINES nil or omitted means do not include the
 11624 height of the mode-, tab- or header-line of WINDOW in the return value.
 11625 If it is the symbol `mode-line', `tab-line' or `header-line', include
 11626 only the height of that line, if present, in the return value.  If t,
 11627 include the height of any of these, if present, in the return value.
 11628 
 11629 IGNORE-LINE-AT-END, if non-nil, means to not add the height of the
 11630 screen line that includes TO to the returned height of the text.  */)
 11631   (Lisp_Object window, Lisp_Object from, Lisp_Object to, Lisp_Object x_limit,
 11632    Lisp_Object y_limit, Lisp_Object mode_lines, Lisp_Object ignore_line_at_end)
 11633 {
 11634   struct window *w = decode_live_window (window);
 11635   struct buffer *b = XBUFFER (w->contents);
 11636   struct buffer *old_b = NULL;
 11637   Lisp_Object value;
 11638 
 11639   if (b != current_buffer)
 11640     {
 11641       old_b = current_buffer;
 11642       set_buffer_internal_1 (b);
 11643     }
 11644 
 11645   value = window_text_pixel_size (window, from, to, x_limit, y_limit, mode_lines,
 11646                                   ignore_line_at_end);
 11647 
 11648   if (old_b)
 11649     set_buffer_internal_1 (old_b);
 11650 
 11651   return value;
 11652 }
 11653 
 11654 DEFUN ("buffer-text-pixel-size", Fbuffer_text_pixel_size, Sbuffer_text_pixel_size, 0, 4, 0,
 11655        doc: /* Return size of whole text of BUFFER-OR-NAME in WINDOW.
 11656 BUFFER-OR-NAME must specify a live buffer or the name of a live buffer
 11657 and defaults to the current buffer.  WINDOW must be a live window and
 11658 defaults to the selected one.  The return value is a cons of the maximum
 11659 pixel-width of any text line and the pixel-height of all the text lines
 11660 of the buffer specified by BUFFER-OR-NAME.
 11661 
 11662 The optional arguments X-LIMIT and Y-LIMIT have the same meaning as with
 11663 `window-text-pixel-size'.
 11664 
 11665 Do not use this function if the buffer specified by BUFFER-OR-NAME is
 11666 already displayed in WINDOW.  `window-text-pixel-size' is cheaper in
 11667 that case because it does not have to temporarily show that buffer in
 11668 WINDOW.  */)
 11669   (Lisp_Object buffer_or_name, Lisp_Object window, Lisp_Object x_limit,
 11670    Lisp_Object y_limit)
 11671 {
 11672   struct window *w = decode_live_window (window);
 11673   struct buffer *b = (NILP (buffer_or_name)
 11674                       ? current_buffer
 11675                       : XBUFFER (Fget_buffer (buffer_or_name)));
 11676   Lisp_Object buffer, value;
 11677   specpdl_ref count = SPECPDL_INDEX ();
 11678 
 11679   XSETBUFFER (buffer, b);
 11680 
 11681   /* The unwind form of with_echo_area_buffer is what we need here to
 11682      make WINDOW temporarily show our buffer.  */
 11683   /* FIXME: Can we move this into the `if (!EQ (buffer, w->contents))`?  */
 11684   record_unwind_protect (unwind_with_echo_area_buffer,
 11685                          with_echo_area_buffer_unwind_data (w));
 11686 
 11687   set_buffer_internal_1 (b);
 11688 
 11689   ptrdiff_t base_line_pos = w->base_line_pos;
 11690   int end_valid = w->window_end_valid;
 11691   if (!EQ (buffer, w->contents))
 11692     {
 11693       wset_buffer (w, buffer);
 11694       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 11695       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 11696     }
 11697 
 11698   value = window_text_pixel_size (window, Qnil, Qnil, x_limit, y_limit, Qnil,
 11699                                   Qnil);
 11700 
 11701   unbind_to (count, Qnil);
 11702 
 11703   /* Restore original values.  This is important if this function is
 11704      called from some ':eval' form in the middle of redisplay.  */
 11705   w->base_line_pos = base_line_pos;
 11706   w->window_end_valid = end_valid;
 11707 
 11708   return value;
 11709 }
 11710 
 11711 
 11712 DEFUN ("display--line-is-continued-p", Fdisplay__line_is_continued_p,
 11713        Sdisplay__line_is_continued_p, 0, 0, 0,
 11714        doc: /* Return non-nil if the current screen line is continued on display.  */)
 11715   (void)
 11716 {
 11717   struct buffer *oldb = current_buffer;
 11718   struct window *w = XWINDOW (selected_window);
 11719   enum move_it_result rc = MOVE_POS_MATCH_OR_ZV;
 11720 
 11721   set_buffer_internal_1 (XBUFFER (w->contents));
 11722 
 11723   if (PT < ZV)
 11724     {
 11725       struct text_pos startpos;
 11726       struct it it;
 11727       void *itdata;
 11728       /* Use a marker, since vertical-motion enters redisplay, which can
 11729          trigger fontifications, which in turn could modify buffer text.  */
 11730       Lisp_Object opoint = Fpoint_marker ();
 11731 
 11732       /* Make sure to start from the beginning of the current screen
 11733          line, so that move_it_in_display_line_to counts pixels correctly.  */
 11734       Fvertical_motion (make_fixnum (0), selected_window, Qnil);
 11735       SET_TEXT_POS (startpos, PT, PT_BYTE);
 11736       itdata = bidi_shelve_cache ();
 11737       start_display (&it, w, startpos);
 11738       /* If lines are truncated, no line is continued.  */
 11739       if (it.line_wrap != TRUNCATE)
 11740         {
 11741           it.glyph_row = NULL;
 11742           rc = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 11743         }
 11744       SET_PT_BOTH (marker_position (opoint), marker_byte_position (opoint));
 11745       bidi_unshelve_cache (itdata, false);
 11746     }
 11747   set_buffer_internal_1 (oldb);
 11748 
 11749   return rc == MOVE_LINE_CONTINUED ? Qt : Qnil;
 11750 }
 11751 
 11752 
 11753 /***********************************************************************
 11754                                Messages
 11755  ***********************************************************************/
 11756 
 11757 /* Return the number of arguments the format string FORMAT needs.  */
 11758 
 11759 static ptrdiff_t
 11760 format_nargs (char const *format)
 11761 {
 11762   ptrdiff_t nargs = 0;
 11763   for (char const *p = format; (p = strchr (p, '%')); p++)
 11764     if (p[1] == '%')
 11765       p++;
 11766     else
 11767       nargs++;
 11768   return nargs;
 11769 }
 11770 
 11771 /* Add a message with format string FORMAT and formatted arguments
 11772    to *Messages*.  */
 11773 
 11774 void
 11775 add_to_log (const char *format, ...)
 11776 {
 11777   va_list ap;
 11778   va_start (ap, format);
 11779   vadd_to_log (format, ap);
 11780   va_end (ap);
 11781 }
 11782 
 11783 void
 11784 vadd_to_log (char const *format, va_list ap)
 11785 {
 11786   ptrdiff_t form_nargs = format_nargs (format);
 11787   ptrdiff_t nargs = 1 + form_nargs;
 11788   Lisp_Object args[10];
 11789   eassert (nargs <= ARRAYELTS (args));
 11790   AUTO_STRING (args0, format);
 11791   args[0] = args0;
 11792   for (ptrdiff_t i = 1; i <= nargs; i++)
 11793     args[i] = va_arg (ap, Lisp_Object);
 11794   Lisp_Object msg = Qnil;
 11795   msg = Fformat_message (nargs, args);
 11796 
 11797   ptrdiff_t len = SBYTES (msg) + 1;
 11798   USE_SAFE_ALLOCA;
 11799   char *buffer = SAFE_ALLOCA (len);
 11800   memcpy (buffer, SDATA (msg), len);
 11801 
 11802   message_dolog (buffer, len - 1, true, STRING_MULTIBYTE (msg));
 11803   SAFE_FREE ();
 11804 }
 11805 
 11806 
 11807 /* Output a newline in the *Messages* buffer if "needs" one.  */
 11808 
 11809 void
 11810 message_log_maybe_newline (void)
 11811 {
 11812   if (message_log_need_newline)
 11813     message_dolog ("", 0, true, false);
 11814 }
 11815 
 11816 
 11817 /* Add a string M of length NBYTES to the message log, optionally
 11818    terminated with a newline when NLFLAG is true.  MULTIBYTE, if
 11819    true, means interpret the contents of M as multibyte.  This
 11820    function calls low-level routines in order to bypass text property
 11821    hooks, etc. which might not be safe to run.
 11822 
 11823    This may GC (insert may run before/after change hooks),
 11824    so the buffer M must NOT point to a Lisp string.  */
 11825 
 11826 void
 11827 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
 11828 {
 11829   const unsigned char *msg = (const unsigned char *) m;
 11830 
 11831   if (!NILP (Vmemory_full))
 11832     return;
 11833 
 11834   if (!NILP (Vmessage_log_max))
 11835     {
 11836       struct buffer *oldbuf;
 11837       Lisp_Object oldpoint, oldbegv, oldzv;
 11838       int old_windows_or_buffers_changed = windows_or_buffers_changed;
 11839       ptrdiff_t point_at_end = 0;
 11840       ptrdiff_t zv_at_end = 0;
 11841       Lisp_Object old_deactivate_mark;
 11842 
 11843       old_deactivate_mark = Vdeactivate_mark;
 11844       oldbuf = current_buffer;
 11845 
 11846       /* Sanity check, in case the variable has been set to something
 11847          invalid.  */
 11848       if (! STRINGP (Vmessages_buffer_name))
 11849         Vmessages_buffer_name = build_string ("*Messages*");
 11850       /* Ensure the Messages buffer exists, and switch to it.
 11851          If we created it, set the major-mode.  */
 11852       bool newbuffer = NILP (Fget_buffer (Vmessages_buffer_name));
 11853       Fset_buffer (Fget_buffer_create (Vmessages_buffer_name, Qnil));
 11854       if (newbuffer
 11855           && !NILP (Ffboundp (intern ("messages-buffer-mode"))))
 11856         call0 (intern ("messages-buffer-mode"));
 11857 
 11858       bset_undo_list (current_buffer, Qt);
 11859       bset_cache_long_scans (current_buffer, Qnil);
 11860 
 11861       oldpoint = message_dolog_marker1;
 11862       set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
 11863       oldbegv = message_dolog_marker2;
 11864       set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
 11865       oldzv = message_dolog_marker3;
 11866       set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
 11867 
 11868       if (PT == Z)
 11869         point_at_end = 1;
 11870       if (ZV == Z)
 11871         zv_at_end = 1;
 11872 
 11873       BEGV = BEG;
 11874       BEGV_BYTE = BEG_BYTE;
 11875       ZV = Z;
 11876       ZV_BYTE = Z_BYTE;
 11877       TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11878 
 11879       /* Insert the string--maybe converting multibyte to single byte
 11880          or vice versa, so that all the text fits the buffer.  */
 11881       if (multibyte
 11882           && NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11883         {
 11884           /* Convert a multibyte string to single-byte
 11885              for the *Message* buffer.  */
 11886           for (ptrdiff_t i = 0; i < nbytes; )
 11887             {
 11888               int char_bytes, c = check_char_and_length (msg + i, &char_bytes);
 11889               char work = CHAR_TO_BYTE8 (c);
 11890               insert_1_both (&work, 1, 1, true, false, false);
 11891               i += char_bytes;
 11892             }
 11893         }
 11894       else if (! multibyte
 11895                && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
 11896         {
 11897           /* Convert a single-byte string to multibyte
 11898              for the *Message* buffer.  */
 11899           for (ptrdiff_t i = 0; i < nbytes; i++)
 11900             {
 11901               int c = make_char_multibyte (msg[i]);
 11902               unsigned char str[MAX_MULTIBYTE_LENGTH];
 11903               int char_bytes = CHAR_STRING (c, str);
 11904               insert_1_both ((char *) str, 1, char_bytes, true, false, false);
 11905             }
 11906         }
 11907       else if (nbytes)
 11908         insert_1_both (m, chars_in_text (msg, nbytes), nbytes,
 11909                        true, false, false);
 11910 
 11911       if (nlflag)
 11912         {
 11913           ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
 11914           intmax_t dups;
 11915 
 11916           /* Since we call del_range_both passing false for PREPARE,
 11917              we aren't prepared to run modification hooks (we could
 11918              end up calling modification hooks from another buffer and
 11919              only with AFTER=t, Bug#21824).  */
 11920           specpdl_ref count = SPECPDL_INDEX ();
 11921           specbind (Qinhibit_modification_hooks, Qt);
 11922 
 11923           insert_1_both ("\n", 1, 1, true, false, false);
 11924 
 11925           scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, false);
 11926           this_bol = PT;
 11927           this_bol_byte = PT_BYTE;
 11928 
 11929           /* See if this line duplicates the previous one.
 11930              If so, combine duplicates.  */
 11931           if (this_bol > BEG)
 11932             {
 11933               scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, false);
 11934               prev_bol = PT;
 11935               prev_bol_byte = PT_BYTE;
 11936 
 11937               dups = message_log_check_duplicate (prev_bol_byte,
 11938                                                   this_bol_byte);
 11939               if (dups)
 11940                 {
 11941                   del_range_both (prev_bol, prev_bol_byte,
 11942                                   this_bol, this_bol_byte, false);
 11943                   if (dups > 1)
 11944                     {
 11945                       char dupstr[sizeof " [ times]"
 11946                                   + INT_STRLEN_BOUND (dups)];
 11947 
 11948                       /* If you change this format, don't forget to also
 11949                          change message_log_check_duplicate.  */
 11950                       int duplen = sprintf (dupstr, " [%"PRIdMAX" times]",
 11951                                             dups);
 11952                       TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
 11953                       insert_1_both (dupstr, duplen, duplen,
 11954                                      true, false, true);
 11955                     }
 11956                 }
 11957             }
 11958 
 11959           /* If we have more than the desired maximum number of lines
 11960              in the *Messages* buffer now, delete the oldest ones.
 11961              This is safe because we don't have undo in this buffer.  */
 11962 
 11963           if (FIXNATP (Vmessage_log_max))
 11964             {
 11965               scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
 11966                             -XFIXNAT (Vmessage_log_max) - 1, false);
 11967               del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, false);
 11968             }
 11969 
 11970           unbind_to (count, Qnil);
 11971         }
 11972       BEGV = marker_position (oldbegv);
 11973       BEGV_BYTE = marker_byte_position (oldbegv);
 11974 
 11975       if (zv_at_end)
 11976         {
 11977           ZV = Z;
 11978           ZV_BYTE = Z_BYTE;
 11979         }
 11980       else
 11981         {
 11982           ZV = marker_position (oldzv);
 11983           ZV_BYTE = marker_byte_position (oldzv);
 11984         }
 11985 
 11986       if (point_at_end)
 11987         TEMP_SET_PT_BOTH (Z, Z_BYTE);
 11988       else
 11989         /* We can't do Fgoto_char (oldpoint) because it will run some
 11990            Lisp code.  */
 11991         TEMP_SET_PT_BOTH (marker_position (oldpoint),
 11992                           marker_byte_position (oldpoint));
 11993 
 11994       unchain_marker (XMARKER (oldpoint));
 11995       unchain_marker (XMARKER (oldbegv));
 11996       unchain_marker (XMARKER (oldzv));
 11997 
 11998       /* We called insert_1_both above with its 5th argument (PREPARE)
 11999          false, which prevents insert_1_both from calling
 12000          prepare_to_modify_buffer, which in turns prevents us from
 12001          incrementing windows_or_buffers_changed even if *Messages* is
 12002          shown in some window.  So we must manually set
 12003          windows_or_buffers_changed here to make up for that.  */
 12004       windows_or_buffers_changed = old_windows_or_buffers_changed;
 12005       bset_redisplay (current_buffer);
 12006 
 12007       set_buffer_internal (oldbuf);
 12008 
 12009       message_log_need_newline = !nlflag;
 12010       Vdeactivate_mark = old_deactivate_mark;
 12011     }
 12012 }
 12013 
 12014 
 12015 /* We are at the end of the buffer after just having inserted a newline.
 12016    (Note: We depend on the fact we won't be crossing the gap.)
 12017    Check to see if the most recent message looks a lot like the previous one.
 12018    Return 0 if different, 1 if the new one should just replace it, or a
 12019    value N > 1 if we should also append " [N times]".  */
 12020 
 12021 static intmax_t
 12022 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
 12023 {
 12024   ptrdiff_t i;
 12025   ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
 12026   bool seen_dots = false;
 12027   unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
 12028   unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
 12029 
 12030   for (i = 0; i < len; i++)
 12031     {
 12032       if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
 12033         seen_dots = true;
 12034       if (p1[i] != p2[i])
 12035         return seen_dots;
 12036     }
 12037   p1 += len;
 12038   if (*p1 == '\n')
 12039     return 2;
 12040   if (*p1++ == ' ' && *p1++ == '[')
 12041     {
 12042       char *pend;
 12043       intmax_t n = strtoimax ((char *) p1, &pend, 10);
 12044       if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
 12045         return n + 1;
 12046     }
 12047   return 0;
 12048 }
 12049 
 12050 
 12051 /* Display an echo area message M with a specified length of NBYTES
 12052    bytes.  The string may include null characters.  If M is not a
 12053    string, clear out any existing message, and let the mini-buffer
 12054    text show through.
 12055 
 12056    This function cancels echoing.  */
 12057 
 12058 void
 12059 message3 (Lisp_Object m)
 12060 {
 12061   clear_message (true, true);
 12062   cancel_echoing ();
 12063 
 12064   /* First flush out any partial line written with print.  */
 12065   message_log_maybe_newline ();
 12066   if (STRINGP (m))
 12067     {
 12068       ptrdiff_t nbytes = SBYTES (m);
 12069       bool multibyte = STRING_MULTIBYTE (m);
 12070       char *buffer;
 12071       USE_SAFE_ALLOCA;
 12072       SAFE_ALLOCA_STRING (buffer, m);
 12073       message_dolog (buffer, nbytes, true, multibyte);
 12074       SAFE_FREE ();
 12075     }
 12076   if (! inhibit_message)
 12077     message3_nolog (m);
 12078 }
 12079 
 12080 /* Log the message M to stderr.  Log an empty line if M is not a string.  */
 12081 
 12082 static void
 12083 message_to_stderr (Lisp_Object m)
 12084 {
 12085   if (noninteractive_need_newline)
 12086     {
 12087       noninteractive_need_newline = false;
 12088       errputc ('\n');
 12089     }
 12090   if (STRINGP (m))
 12091     {
 12092       Lisp_Object coding_system = Vlocale_coding_system;
 12093       Lisp_Object s;
 12094 
 12095       if (!NILP (Vcoding_system_for_write))
 12096         coding_system = Vcoding_system_for_write;
 12097       if (!NILP (coding_system))
 12098         s = code_convert_string_norecord (m, coding_system, true);
 12099       else
 12100         s = m;
 12101 
 12102       errwrite (SDATA (s), SBYTES (s));
 12103     }
 12104   if (STRINGP (m) || !cursor_in_echo_area)
 12105     errputc ('\n');
 12106 }
 12107 
 12108 /* The non-logging version of message3.
 12109    This does not cancel echoing, because it is used for echoing.
 12110    Perhaps we need to make a separate function for echoing
 12111    and make this cancel echoing.  */
 12112 
 12113 void
 12114 message3_nolog (Lisp_Object m)
 12115 {
 12116   struct frame *sf = SELECTED_FRAME ();
 12117 
 12118   if (FRAME_INITIAL_P (sf))
 12119     message_to_stderr (m);
 12120   /* Error messages get reported properly by cmd_error, so this must be just an
 12121      informative message; if the frame hasn't really been initialized yet, just
 12122      toss it.  */
 12123   else if (INTERACTIVE && sf->glyphs_initialized_p)
 12124     {
 12125       /* Get the frame containing the mini-buffer
 12126          that the selected frame is using.  */
 12127       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 12128       Lisp_Object frame = XWINDOW (mini_window)->frame;
 12129       struct frame *f = XFRAME (frame);
 12130 
 12131       if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
 12132         Fmake_frame_visible (frame);
 12133 
 12134       if (STRINGP (m) && SCHARS (m) > 0)
 12135         {
 12136           set_message (m);
 12137           if (minibuffer_auto_raise)
 12138             Fraise_frame (frame);
 12139           /* Assume we are not echoing.
 12140              (If we are, echo_now will override this.)  */
 12141           echo_message_buffer = Qnil;
 12142         }
 12143       else
 12144         clear_message (true, true);
 12145 
 12146       do_pending_window_change (false);
 12147       echo_area_display (true);
 12148       do_pending_window_change (false);
 12149       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 12150         (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
 12151     }
 12152 }
 12153 
 12154 
 12155 /* Display a null-terminated echo area message M.  If M is 0, clear
 12156    out any existing message, and let the mini-buffer text show through.
 12157 
 12158    The buffer M must continue to exist until after the echo area gets
 12159    cleared or some other message gets displayed there.  Do not pass
 12160    text that is stored in a Lisp string.  Do not pass text in a buffer
 12161    that was alloca'd.  */
 12162 
 12163 void
 12164 message1 (const char *m)
 12165 {
 12166   message3 (m ? build_unibyte_string (m) : Qnil);
 12167 }
 12168 
 12169 
 12170 /* The non-logging counterpart of message1.  */
 12171 
 12172 void
 12173 message1_nolog (const char *m)
 12174 {
 12175   message3_nolog (m ? build_unibyte_string (m) : Qnil);
 12176 }
 12177 
 12178 /* Display a message M which contains a single %s
 12179    which gets replaced with STRING.  */
 12180 
 12181 void
 12182 message_with_string (const char *m, Lisp_Object string, bool log)
 12183 {
 12184   CHECK_STRING (string);
 12185 
 12186   bool need_message;
 12187   if (noninteractive)
 12188     need_message = !!m;
 12189   else if (!INTERACTIVE)
 12190     need_message = false;
 12191   else
 12192     {
 12193       /* The frame whose minibuffer we're going to display the message on.
 12194          It may be larger than the selected frame, so we need
 12195          to use its buffer, not the selected frame's buffer.  */
 12196       Lisp_Object mini_window;
 12197       struct frame *f, *sf = SELECTED_FRAME ();
 12198 
 12199       /* Get the frame containing the minibuffer
 12200          that the selected frame is using.  */
 12201       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12202       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12203 
 12204       /* Error messages get reported properly by cmd_error, so this must be
 12205          just an informative message; if the frame hasn't really been
 12206          initialized yet, just toss it.  */
 12207       need_message = f->glyphs_initialized_p;
 12208     }
 12209 
 12210   if (need_message)
 12211     {
 12212       AUTO_STRING (fmt, m);
 12213       Lisp_Object msg = CALLN (Fformat_message, fmt, string);
 12214 
 12215       if (noninteractive)
 12216         message_to_stderr (msg);
 12217       else
 12218         {
 12219           if (log)
 12220             message3 (msg);
 12221           else
 12222             message3_nolog (msg);
 12223 
 12224           /* Print should start at the beginning of the message
 12225              buffer next time.  */
 12226           message_buf_print = false;
 12227         }
 12228     }
 12229 }
 12230 
 12231 
 12232 /* Dump an informative message to the minibuf.  If M is 0, clear out
 12233    any existing message, and let the mini-buffer text show through.
 12234 
 12235    The message must be safe ASCII (because when Emacs is
 12236    non-interactive the message is sent straight to stderr without
 12237    encoding first) and the format must not contain ` or ' (because
 12238    this function does not account for `text-quoting-style').  If your
 12239    message and format do not fit into this category, convert your
 12240    arguments to Lisp objects and use Fmessage instead.  */
 12241 
 12242 static void ATTRIBUTE_FORMAT_PRINTF (1, 0)
 12243 vmessage (const char *m, va_list ap)
 12244 {
 12245   if (noninteractive)
 12246     {
 12247       if (m)
 12248         {
 12249           if (noninteractive_need_newline)
 12250             putc ('\n', stderr);
 12251           noninteractive_need_newline = false;
 12252           vfprintf (stderr, m, ap);
 12253           if (!cursor_in_echo_area)
 12254             putc ('\n', stderr);
 12255           fflush (stderr);
 12256         }
 12257     }
 12258   else if (INTERACTIVE)
 12259     {
 12260       /* The frame whose mini-buffer we're going to display the message
 12261          on.  It may be larger than the selected frame, so we need to
 12262          use its buffer, not the selected frame's buffer.  */
 12263       Lisp_Object mini_window;
 12264       struct frame *f, *sf = SELECTED_FRAME ();
 12265 
 12266       /* Get the frame containing the mini-buffer
 12267          that the selected frame is using.  */
 12268       mini_window = FRAME_MINIBUF_WINDOW (sf);
 12269       f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 12270 
 12271       /* Error messages get reported properly by cmd_error, so this must be
 12272          just an informative message; if the frame hasn't really been
 12273          initialized yet, just toss it.  */
 12274       if (f->glyphs_initialized_p)
 12275         {
 12276           if (m)
 12277             {
 12278               ptrdiff_t len;
 12279               ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
 12280               USE_SAFE_ALLOCA;
 12281               char *message_buf = SAFE_ALLOCA (maxsize + 1);
 12282 
 12283               len = doprnt (message_buf, maxsize, m, 0, ap);
 12284 
 12285               message3 (make_string (message_buf, len));
 12286               SAFE_FREE ();
 12287             }
 12288           else
 12289             message1 (0);
 12290 
 12291           /* Print should start at the beginning of the message
 12292              buffer next time.  */
 12293           message_buf_print = false;
 12294         }
 12295     }
 12296 }
 12297 
 12298 /* See vmessage for restrictions on the text of the message.  */
 12299 void
 12300 message (const char *m, ...)
 12301 {
 12302   va_list ap;
 12303   va_start (ap, m);
 12304   vmessage (m, ap);
 12305   va_end (ap);
 12306 }
 12307 
 12308 
 12309 /* Display the current message in the current mini-buffer.  This is
 12310    only called from error handlers in process.c, and is not time
 12311    critical.  */
 12312 
 12313 void
 12314 update_echo_area (void)
 12315 {
 12316   if (!NILP (echo_area_buffer[0]))
 12317     {
 12318       Lisp_Object string;
 12319       string = Fcurrent_message ();
 12320       message3 (string);
 12321     }
 12322 }
 12323 
 12324 
 12325 /* Make sure echo area buffers in `echo_buffers' are live.
 12326    If they aren't, make new ones.  */
 12327 
 12328 static void
 12329 ensure_echo_area_buffers (void)
 12330 {
 12331   for (int i = 0; i < 2; i++)
 12332     if (!BUFFERP (echo_buffer[i])
 12333         || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
 12334       {
 12335         Lisp_Object old_buffer = echo_buffer[i];
 12336         static char const name_fmt[] = " *Echo Area %d*";
 12337         char name[sizeof name_fmt + INT_STRLEN_BOUND (int)];
 12338         AUTO_STRING_WITH_LEN (lname, name, sprintf (name, name_fmt, i));
 12339         echo_buffer[i] = Fget_buffer_create (lname, Qnil);
 12340         bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
 12341         /* to force word wrap in echo area -
 12342            it was decided to postpone this*/
 12343         /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
 12344 
 12345         for (int j = 0; j < 2; j++)
 12346           if (EQ (old_buffer, echo_area_buffer[j]))
 12347             echo_area_buffer[j] = echo_buffer[i];
 12348       }
 12349 }
 12350 
 12351 
 12352 /* Call FN with args A1..A2 with either the current or last displayed
 12353    echo_area_buffer as current buffer.
 12354 
 12355    WHICH zero means use the current message buffer
 12356    echo_area_buffer[0].  If that is nil, choose a suitable buffer
 12357    from echo_buffer[] and clear it.
 12358 
 12359    WHICH > 0 means use echo_area_buffer[1].  If that is nil, choose a
 12360    suitable buffer from echo_buffer[] and clear it.
 12361 
 12362    If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
 12363    that the current message becomes the last displayed one, choose a
 12364    suitable buffer for echo_area_buffer[0], and clear it.
 12365 
 12366    Value is what FN returns.  */
 12367 
 12368 static bool
 12369 with_echo_area_buffer (struct window *w, int which,
 12370                        bool (*fn) (void *, Lisp_Object),
 12371                        void *a1, Lisp_Object a2)
 12372 {
 12373   Lisp_Object buffer;
 12374   bool this_one, the_other, clear_buffer_p, rc;
 12375   specpdl_ref count = SPECPDL_INDEX ();
 12376 
 12377   /* If buffers aren't live, make new ones.  */
 12378   ensure_echo_area_buffers ();
 12379 
 12380   clear_buffer_p = false;
 12381 
 12382   if (which == 0)
 12383     this_one = false, the_other = true;
 12384   else if (which > 0)
 12385     this_one = true, the_other = false;
 12386   else
 12387     {
 12388       this_one = false, the_other = true;
 12389       clear_buffer_p = true;
 12390 
 12391       /* We need a fresh one in case the current echo buffer equals
 12392          the one containing the last displayed echo area message.  */
 12393       if (!NILP (echo_area_buffer[this_one])
 12394           && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
 12395         echo_area_buffer[this_one] = Qnil;
 12396     }
 12397 
 12398   /* Choose a suitable buffer from echo_buffer[] if we don't
 12399      have one.  */
 12400   if (NILP (echo_area_buffer[this_one]))
 12401     {
 12402       echo_area_buffer[this_one]
 12403         = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
 12404            ? echo_buffer[the_other]
 12405            : echo_buffer[this_one]);
 12406       clear_buffer_p = true;
 12407     }
 12408 
 12409   buffer = echo_area_buffer[this_one];
 12410 
 12411   /* Don't get confused by reusing the buffer used for echoing
 12412      for a different purpose.  */
 12413   if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
 12414     cancel_echoing ();
 12415 
 12416   record_unwind_protect (unwind_with_echo_area_buffer,
 12417                          with_echo_area_buffer_unwind_data (w));
 12418 
 12419   /* Make the echo area buffer current.  Note that for display
 12420      purposes, it is not necessary that the displayed window's buffer
 12421      == current_buffer, except for text property lookup.  So, let's
 12422      only set that buffer temporarily here without doing a full
 12423      Fset_window_buffer.  We must also change w->pointm, though,
 12424      because otherwise an assertions in unshow_buffer fails, and Emacs
 12425      aborts.  */
 12426   set_buffer_internal_1 (XBUFFER (buffer));
 12427   if (w)
 12428     {
 12429       wset_buffer (w, buffer);
 12430       set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
 12431       set_marker_both (w->old_pointm, buffer, BEG, BEG_BYTE);
 12432     }
 12433 
 12434   bset_undo_list (current_buffer, Qt);
 12435   bset_read_only (current_buffer, Qnil);
 12436   specbind (Qinhibit_read_only, Qt);
 12437   specbind (Qinhibit_modification_hooks, Qt);
 12438 
 12439   if (clear_buffer_p && Z > BEG)
 12440     del_range (BEG, Z);
 12441 
 12442   eassert (BEGV >= BEG);
 12443   eassert (ZV <= Z && ZV >= BEGV);
 12444 
 12445   rc = fn (a1, a2);
 12446 
 12447   eassert (BEGV >= BEG);
 12448   eassert (ZV <= Z && ZV >= BEGV);
 12449 
 12450   unbind_to (count, Qnil);
 12451   return rc;
 12452 }
 12453 
 12454 
 12455 /* Save state that should be preserved around the call to the function
 12456    FN called in with_echo_area_buffer.  */
 12457 
 12458 static Lisp_Object
 12459 with_echo_area_buffer_unwind_data (struct window *w)
 12460 {
 12461   int i = 0;
 12462   Lisp_Object vector, tmp;
 12463 
 12464   /* Reduce consing by keeping one vector in
 12465      Vwith_echo_area_save_vector.  */
 12466   vector = Vwith_echo_area_save_vector;
 12467   Vwith_echo_area_save_vector = Qnil;
 12468 
 12469   if (NILP (vector))
 12470     vector = make_nil_vector (11);
 12471 
 12472   XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
 12473   ASET (vector, i, Vdeactivate_mark); ++i;
 12474   ASET (vector, i, make_fixnum (windows_or_buffers_changed)); ++i;
 12475 
 12476   if (w)
 12477     {
 12478       XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
 12479       ASET (vector, i, w->contents); ++i;
 12480       ASET (vector, i, make_fixnum (marker_position (w->pointm))); ++i;
 12481       ASET (vector, i, make_fixnum (marker_byte_position (w->pointm))); ++i;
 12482       ASET (vector, i, make_fixnum (marker_position (w->old_pointm))); ++i;
 12483       ASET (vector, i, make_fixnum (marker_byte_position (w->old_pointm))); ++i;
 12484       ASET (vector, i, make_fixnum (marker_position (w->start))); ++i;
 12485       ASET (vector, i, make_fixnum (marker_byte_position (w->start))); ++i;
 12486     }
 12487   else
 12488     {
 12489       int end = i + 8;
 12490       for (; i < end; ++i)
 12491         ASET (vector, i, Qnil);
 12492     }
 12493 
 12494   eassert (i == ASIZE (vector));
 12495   return vector;
 12496 }
 12497 
 12498 
 12499 /* Restore global state from VECTOR which was created by
 12500    with_echo_area_buffer_unwind_data.  */
 12501 
 12502 static void
 12503 unwind_with_echo_area_buffer (Lisp_Object vector)
 12504 {
 12505   set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
 12506   Vdeactivate_mark = AREF (vector, 1);
 12507   windows_or_buffers_changed = XFIXNAT (AREF (vector, 2));
 12508 
 12509   if (WINDOWP (AREF (vector, 3)))
 12510     {
 12511       struct window *w;
 12512       Lisp_Object buffer;
 12513 
 12514       w = XWINDOW (AREF (vector, 3));
 12515       buffer = AREF (vector, 4);
 12516 
 12517       wset_buffer (w, buffer);
 12518       set_marker_restricted_both (w->pointm, buffer,
 12519                                   XFIXNAT (AREF (vector, 5)),
 12520                                   XFIXNAT (AREF (vector, 6)));
 12521       set_marker_restricted_both (w->old_pointm, buffer,
 12522                                   XFIXNAT (AREF (vector, 7)),
 12523                                   XFIXNAT (AREF (vector, 8)));
 12524       set_marker_restricted_both (w->start, buffer,
 12525                                   XFIXNAT (AREF (vector, 9)),
 12526                                   XFIXNAT (AREF (vector, 10)));
 12527     }
 12528 
 12529   Vwith_echo_area_save_vector = vector;
 12530 }
 12531 
 12532 
 12533 /* Set up the echo area for use by print functions.  MULTIBYTE_P
 12534    means we will print multibyte.  */
 12535 
 12536 void
 12537 setup_echo_area_for_printing (bool multibyte_p)
 12538 {
 12539   /* If we can't find an echo area any more, exit.  */
 12540   if (! FRAME_LIVE_P (XFRAME (selected_frame)))
 12541     Fkill_emacs (Qnil, Qnil);
 12542 
 12543   ensure_echo_area_buffers ();
 12544 
 12545   if (!message_buf_print)
 12546     {
 12547       /* A message has been output since the last time we printed.
 12548          Choose a fresh echo area buffer.  */
 12549       if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12550         echo_area_buffer[0] = echo_buffer[1];
 12551       else
 12552         echo_area_buffer[0] = echo_buffer[0];
 12553 
 12554       /* Switch to that buffer and clear it.  */
 12555       set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12556       bset_truncate_lines (current_buffer, Qnil);
 12557 
 12558       if (Z > BEG)
 12559         {
 12560           specpdl_ref count = SPECPDL_INDEX ();
 12561           specbind (Qinhibit_read_only, Qt);
 12562           /* Note that undo recording is always disabled.  */
 12563           del_range (BEG, Z);
 12564           unbind_to (count, Qnil);
 12565         }
 12566       TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 12567 
 12568       /* Set up the buffer for the multibyteness we need.  We always
 12569          set it to be multibyte, except when
 12570          unibyte-display-via-language-environment is non-nil and the
 12571          buffer from which we are called is unibyte, because in that
 12572          case unibyte characters should not be displayed as octal
 12573          escapes.  */
 12574       if (unibyte_display_via_language_environment
 12575           && !multibyte_p
 12576           && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12577         Fset_buffer_multibyte (Qnil);
 12578       else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 12579         Fset_buffer_multibyte (Qt);
 12580 
 12581       /* Raise the frame containing the echo area.  */
 12582       if (minibuffer_auto_raise)
 12583         {
 12584           struct frame *sf = SELECTED_FRAME ();
 12585           Lisp_Object mini_window;
 12586           mini_window = FRAME_MINIBUF_WINDOW (sf);
 12587           Fraise_frame  (WINDOW_FRAME (XWINDOW (mini_window)));
 12588         }
 12589 
 12590       message_log_maybe_newline ();
 12591       message_buf_print = true;
 12592     }
 12593   else
 12594     {
 12595       if (NILP (echo_area_buffer[0]))
 12596         {
 12597           if (EQ (echo_area_buffer[1], echo_buffer[0]))
 12598             echo_area_buffer[0] = echo_buffer[1];
 12599           else
 12600             echo_area_buffer[0] = echo_buffer[0];
 12601         }
 12602 
 12603       if (current_buffer != XBUFFER (echo_area_buffer[0]))
 12604         {
 12605           /* Someone switched buffers between print requests.  */
 12606           set_buffer_internal (XBUFFER (echo_area_buffer[0]));
 12607           bset_truncate_lines (current_buffer, Qnil);
 12608         }
 12609     }
 12610 }
 12611 
 12612 
 12613 /* Display an echo area message in window W.  Value is true if W's
 12614    height is changed.  If display_last_displayed_message_p,
 12615    display the message that was last displayed, otherwise
 12616    display the current message.  */
 12617 
 12618 static bool
 12619 display_echo_area (struct window *w)
 12620 {
 12621   bool no_message_p, window_height_changed_p;
 12622 
 12623   /* Temporarily disable garbage collections while displaying the echo
 12624      area.  This is done because a GC can print a message itself.
 12625      That message would modify the echo area buffer's contents while a
 12626      redisplay of the buffer is going on, and seriously confuse
 12627      redisplay.  */
 12628   specpdl_ref count = inhibit_garbage_collection ();
 12629 
 12630   /* If there is no message, we must call display_echo_area_1
 12631      nevertheless because it resizes the window.  But we will have to
 12632      reset the echo_area_buffer in question to nil at the end because
 12633      with_echo_area_buffer will set it to an empty buffer.  */
 12634   bool i = display_last_displayed_message_p;
 12635   /* According to the C standard, the integral value
 12636      of a "bool" is always 0 or 1, so this array access is safe here,
 12637      if oddly typed. */
 12638   no_message_p = NILP (echo_area_buffer[i]);
 12639 
 12640   window_height_changed_p
 12641     = with_echo_area_buffer (w, display_last_displayed_message_p,
 12642                              display_echo_area_1, w, Qnil);
 12643 
 12644   if (no_message_p)
 12645     echo_area_buffer[i] = Qnil;
 12646 
 12647   unbind_to (count, Qnil);
 12648   return window_height_changed_p;
 12649 }
 12650 
 12651 
 12652 /* Helper for display_echo_area.  Display the current buffer which
 12653    contains the current echo area message in window W, a mini-window,
 12654    a pointer to which is passed in A1.  A2 is currently not used.
 12655    Change the height of W so that all of the message is displayed.
 12656    Value is true if height of W was changed.  */
 12657 
 12658 static bool
 12659 display_echo_area_1 (void *a1, Lisp_Object a2)
 12660 {
 12661   struct window *w = a1;
 12662   Lisp_Object window;
 12663   struct text_pos start;
 12664 
 12665   /* We are about to enter redisplay without going through
 12666      redisplay_internal, so we need to forget these faces by hand
 12667      here.  */
 12668   forget_escape_and_glyphless_faces ();
 12669 
 12670   /* Do this before displaying, so that we have a large enough glyph
 12671      matrix for the display.  If we can't get enough space for the
 12672      whole text, display the last N lines.  That works by setting w->start.  */
 12673   bool window_height_changed_p = resize_mini_window (w, false);
 12674 
 12675   /* Use the starting position chosen by resize_mini_window.  */
 12676   SET_TEXT_POS_FROM_MARKER (start, w->start);
 12677 
 12678   /* Display.  */
 12679   clear_glyph_matrix (w->desired_matrix);
 12680   XSETWINDOW (window, w);
 12681   void *itdata = bidi_shelve_cache ();
 12682   try_window (window, start, 0);
 12683   bidi_unshelve_cache (itdata, false);
 12684 
 12685   return window_height_changed_p;
 12686 }
 12687 
 12688 
 12689 /* Resize the echo area window to exactly the size needed for the
 12690    currently displayed message, if there is one.  If a mini-buffer
 12691    is active, don't shrink it.  */
 12692 
 12693 void
 12694 resize_echo_area_exactly (void)
 12695 {
 12696   if (BUFFERP (echo_area_buffer[0])
 12697       && WINDOWP (echo_area_window))
 12698     {
 12699       struct window *w = XWINDOW (echo_area_window);
 12700       Lisp_Object resize_exactly = (minibuf_level == 0 ? Qt : Qnil);
 12701       bool resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
 12702                                               w, resize_exactly);
 12703       if (resized_p)
 12704         {
 12705           windows_or_buffers_changed = 42;
 12706           update_mode_lines = 30;
 12707           redisplay_internal ();
 12708         }
 12709     }
 12710 }
 12711 
 12712 
 12713 /* Callback function for with_echo_area_buffer, when used from
 12714    resize_echo_area_exactly.  A1 contains a pointer to the window to
 12715    resize, EXACTLY non-nil means resize the mini-window exactly to the
 12716    size of the text displayed.  Value is what resize_mini_window
 12717    returns.  */
 12718 
 12719 static bool
 12720 resize_mini_window_1 (void *a1, Lisp_Object exactly)
 12721 {
 12722   return resize_mini_window (a1, !NILP (exactly));
 12723 }
 12724 
 12725 
 12726 /* Resize mini-window W to fit the size of its contents.  EXACT_P
 12727    means size the window exactly to the size needed.  Otherwise, it's
 12728    only enlarged until W's buffer is empty.
 12729 
 12730    Set W->start to the right place to begin display.  If the whole
 12731    contents fit, start at the beginning.  Otherwise, start so as
 12732    to make the end of the contents appear.  This is particularly
 12733    important for y-or-n-p, but seems desirable generally.
 12734 
 12735    Value is true if the window height has been changed.  */
 12736 
 12737 bool
 12738 resize_mini_window (struct window *w, bool exact_p)
 12739 {
 12740   struct frame *f = XFRAME (w->frame);
 12741   int old_height = WINDOW_BOX_TEXT_HEIGHT (w);
 12742 
 12743   eassert (MINI_WINDOW_P (w));
 12744 
 12745   /* Don't resize windows while redisplaying a window; it would
 12746      confuse redisplay functions when the size of the window they are
 12747      displaying changes from under them.  Such a resizing can happen,
 12748      for instance, when which-func prints a long message while
 12749      we are running fontification-functions.  We're running these
 12750      functions with safe_call which binds inhibit-redisplay to t.  */
 12751   if (!NILP (Vinhibit_redisplay))
 12752     return false;
 12753 
 12754   /* By default, start display at the beginning.  */
 12755   if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12756     set_marker_both (w->start, w->contents,
 12757                      BUF_BEGV (XBUFFER (w->contents)),
 12758                      BUF_BEGV_BYTE (XBUFFER (w->contents)));
 12759 
 12760   /* Nil means don't try to resize.  */
 12761   if ((NILP (Vresize_mini_windows)
 12762        && (NILP (resize_mini_frames) || !FRAME_MINIBUF_ONLY_P (f)))
 12763       || (FRAME_X_P (f) && FRAME_OUTPUT_DATA (f) == NULL))
 12764     return false;
 12765 
 12766   if (FRAME_MINIBUF_ONLY_P (f))
 12767     {
 12768       if (!NILP (resize_mini_frames))
 12769         safe_call1 (Qwindow__resize_mini_frame, WINDOW_FRAME (w));
 12770     }
 12771   else
 12772     {
 12773       struct it it;
 12774       int unit = FRAME_LINE_HEIGHT (f);
 12775       int height, max_height;
 12776       struct text_pos start;
 12777       struct buffer *old_current_buffer = NULL;
 12778       int windows_height = FRAME_INNER_HEIGHT (f);
 12779 
 12780       if (current_buffer != XBUFFER (w->contents))
 12781         {
 12782           old_current_buffer = current_buffer;
 12783           set_buffer_internal (XBUFFER (w->contents));
 12784         }
 12785 
 12786       init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
 12787 
 12788       /* Compute the max. number of lines specified by the user.  */
 12789       if (FLOATP (Vmax_mini_window_height))
 12790         max_height = XFLOAT_DATA (Vmax_mini_window_height) * windows_height;
 12791       else if (FIXNUMP (Vmax_mini_window_height))
 12792         max_height = XFIXNUM (Vmax_mini_window_height) * unit;
 12793       else
 12794         max_height = windows_height / 4;
 12795 
 12796       /* Correct that max. height if it's bogus.  */
 12797       max_height = clip_to_bounds (unit, max_height, windows_height);
 12798 
 12799       /* Find out the height of the text in the window.  */
 12800       last_height = 0;
 12801       move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
 12802       /* If move_it_to moved to the next visible line after EOB,
 12803          account for the height of the last full line.  */
 12804       if (it.max_ascent == 0 && it.max_descent == 0)
 12805         {
 12806           height = it.current_y;
 12807           /* Don't add the last line's height if lines are truncated
 12808              and the text doesn't end in a newline.
 12809              FIXME: if the text ends in a newline from a display
 12810              property or an overlay string, they lose: the mini-window
 12811              might not show the last empty line.  */
 12812           if (!(it.line_wrap == TRUNCATE
 12813                 && it.current_x <= it.first_visible_x
 12814                 && ZV_BYTE > 1
 12815                 && FETCH_BYTE (ZV_BYTE - 1) != '\n'))
 12816             height += last_height;
 12817         }
 12818       else
 12819         height = it.current_y + it.max_ascent + it.max_descent;
 12820       height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
 12821 
 12822       /* Compute a suitable window start.  */
 12823       if (height > max_height)
 12824         {
 12825           height = (max_height / unit) * unit;
 12826           if (redisplay_adhoc_scroll_in_resize_mini_windows)
 12827             {
 12828               init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
 12829               move_it_vertically_backward (&it, height - unit);
 12830               /* The following move is usually a no-op when the stuff
 12831                  displayed in the mini-window comes entirely from buffer
 12832                  text, but it is needed when some of it comes from overlay
 12833                  strings, especially when there's an after-string at ZV.
 12834                  This happens with some completion packages, like
 12835                  icomplete, ido-vertical, etc.  With those packages, if we
 12836                  don't force w->start to be at the beginning of a screen
 12837                  line, important parts of the stuff in the mini-window,
 12838                  such as user prompt, will be hidden from view.  */
 12839               move_it_by_lines (&it, 0);
 12840               start = it.current.pos;
 12841               /* Prevent redisplay_window from recentering, and thus from
 12842                  overriding the window-start point we computed here.  */
 12843               w->start_at_line_beg = false;
 12844               SET_MARKER_FROM_TEXT_POS (w->start, start);
 12845             }
 12846         }
 12847       else
 12848         {
 12849           SET_TEXT_POS (start, BEGV, BEGV_BYTE);
 12850           SET_MARKER_FROM_TEXT_POS (w->start, start);
 12851         }
 12852 
 12853       if (EQ (Vresize_mini_windows, Qgrow_only))
 12854         {
 12855           /* Let it grow only, until we display an empty message, in which
 12856              case the window shrinks again.  */
 12857           if (height > old_height)
 12858             grow_mini_window (w, height - old_height);
 12859           else if (height < old_height && (exact_p || BEGV == ZV))
 12860             shrink_mini_window (w);
 12861         }
 12862       else if (height != old_height)
 12863         /* Always resize to exact size needed.  */
 12864         grow_mini_window (w, height - old_height);
 12865 
 12866       if (old_current_buffer)
 12867         set_buffer_internal (old_current_buffer);
 12868     }
 12869 
 12870   return WINDOW_BOX_TEXT_HEIGHT (w) != old_height;
 12871 }
 12872 
 12873 
 12874 /* Value is the current message, a string, or nil if there is no
 12875    current message.  */
 12876 
 12877 Lisp_Object
 12878 current_message (void)
 12879 {
 12880   Lisp_Object msg;
 12881 
 12882   if (!BUFFERP (echo_area_buffer[0]))
 12883     msg = Qnil;
 12884   else
 12885     {
 12886       with_echo_area_buffer (0, 0, current_message_1, &msg, Qnil);
 12887       if (NILP (msg))
 12888         echo_area_buffer[0] = Qnil;
 12889     }
 12890 
 12891   return msg;
 12892 }
 12893 
 12894 
 12895 static bool
 12896 current_message_1 (void *a1, Lisp_Object a2)
 12897 {
 12898   Lisp_Object *msg = a1;
 12899 
 12900   if (Z > BEG)
 12901     *msg = make_buffer_string (BEG, Z, true);
 12902   else
 12903     *msg = Qnil;
 12904   return false;
 12905 }
 12906 
 12907 
 12908 /* Push the current message on Vmessage_stack for later restoration
 12909    by restore_message.  Value is true if the current message isn't
 12910    empty.  This is a relatively infrequent operation, so it's not
 12911    worth optimizing.  */
 12912 
 12913 bool
 12914 push_message (void)
 12915 {
 12916   Lisp_Object msg = current_message ();
 12917   Vmessage_stack = Fcons (msg, Vmessage_stack);
 12918   return STRINGP (msg);
 12919 }
 12920 
 12921 
 12922 /* Restore message display from the top of Vmessage_stack.  */
 12923 
 12924 void
 12925 restore_message (void)
 12926 {
 12927   eassert (CONSP (Vmessage_stack));
 12928   message3_nolog (XCAR (Vmessage_stack));
 12929 }
 12930 
 12931 
 12932 /* Handler for unwind-protect calling pop_message.  */
 12933 
 12934 void
 12935 pop_message_unwind (void)
 12936 {
 12937   /* Pop the top-most entry off Vmessage_stack.  */
 12938   eassert (CONSP (Vmessage_stack));
 12939   Vmessage_stack = XCDR (Vmessage_stack);
 12940 }
 12941 
 12942 
 12943 /* Check that Vmessage_stack is nil.  Called from emacs.c when Emacs
 12944    exits.  If the stack is not empty, we have a missing
 12945    pop_message_unwind somewhere.  */
 12946 
 12947 void
 12948 check_message_stack (void)
 12949 {
 12950   if (!NILP (Vmessage_stack))
 12951     emacs_abort ();
 12952 }
 12953 
 12954 void
 12955 clear_message_stack (void)
 12956 {
 12957   Vmessage_stack = Qnil;
 12958 }
 12959 
 12960 /* Truncate to NCHARS what will be displayed in the echo area the next
 12961    time we display it---but don't redisplay it now.  */
 12962 
 12963 void
 12964 truncate_echo_area (ptrdiff_t nchars)
 12965 {
 12966   if (nchars == 0)
 12967     echo_area_buffer[0] = Qnil;
 12968   else if (!noninteractive
 12969            && INTERACTIVE
 12970            && !NILP (echo_area_buffer[0]))
 12971     {
 12972       struct frame *sf = SELECTED_FRAME ();
 12973       /* Error messages get reported properly by cmd_error, so this must be
 12974          just an informative message; if the frame hasn't really been
 12975          initialized yet, just toss it.  */
 12976       if (sf->glyphs_initialized_p)
 12977         with_echo_area_buffer (0, 0, truncate_message_1,
 12978                                (void *) (intptr_t) nchars, Qnil);
 12979     }
 12980 }
 12981 
 12982 
 12983 /* Helper function for truncate_echo_area.  Truncate the current
 12984    message to at most NCHARS characters.  */
 12985 
 12986 static bool
 12987 truncate_message_1 (void *a1, Lisp_Object a2)
 12988 {
 12989   intptr_t nchars = (intptr_t) a1;
 12990   if (BEG + nchars < Z)
 12991     del_range (BEG + nchars, Z);
 12992   if (Z == BEG)
 12993     echo_area_buffer[0] = Qnil;
 12994   return false;
 12995 }
 12996 
 12997 extern intptr_t garbage_collection_inhibited;
 12998 
 12999 /* Set the current message to STRING.  */
 13000 
 13001 static void
 13002 set_message (Lisp_Object string)
 13003 {
 13004   Lisp_Object message = Qnil;
 13005 
 13006   eassert (STRINGP (string));
 13007 
 13008   if (FUNCTIONP (Vset_message_function)
 13009       /* FIXME: (bug#63253) We should really make the regexp engine re-entrant,
 13010          but in the mean time, let's ignore `set-message-function` when
 13011          called from `probably_quit`.  */
 13012       && !garbage_collection_inhibited)
 13013     {
 13014       specpdl_ref count = SPECPDL_INDEX ();
 13015       specbind (Qinhibit_quit, Qt);
 13016       message = safe_call1 (Vset_message_function, string);
 13017       unbind_to (count, Qnil);
 13018 
 13019       if (STRINGP (message))
 13020         {
 13021           string = message;
 13022           message = Qnil;
 13023         }
 13024     }
 13025 
 13026   if (NILP (message))
 13027     {
 13028       message_enable_multibyte = STRING_MULTIBYTE (string);
 13029 
 13030       with_echo_area_buffer (0, -1, set_message_1, 0, string);
 13031       message_buf_print = false;
 13032       help_echo_showing_p = false;
 13033     }
 13034 
 13035   if (STRINGP (Vdebug_on_message)
 13036       && STRINGP (string)
 13037       && fast_string_match (Vdebug_on_message, string) >= 0)
 13038     call_debugger (list2 (Qerror, string));
 13039 }
 13040 
 13041 
 13042 /* Helper function for set_message.  First argument is ignored and second
 13043    argument has the same meaning as for set_message.
 13044    This function is called with the echo area buffer being current.  */
 13045 
 13046 static bool
 13047 set_message_1 (void *a1, Lisp_Object string)
 13048 {
 13049   eassert (STRINGP (string));
 13050 
 13051   /* Change multibyteness of the echo buffer appropriately.  We always
 13052      set it to be multibyte, except when
 13053      unibyte-display-via-language-environment is non-nil and the
 13054      string to display is unibyte, because in that case unibyte
 13055      characters should not be displayed as octal escapes.  */
 13056   if (!message_enable_multibyte
 13057       && unibyte_display_via_language_environment
 13058       && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13059     Fset_buffer_multibyte (Qnil);
 13060   else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
 13061     Fset_buffer_multibyte (Qt);
 13062 
 13063   bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
 13064   if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
 13065     bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
 13066 
 13067   /* Insert new message at BEG.  */
 13068   TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
 13069 
 13070   /* This function takes care of single/multibyte conversion.
 13071      We just have to ensure that the echo area buffer has the right
 13072      setting of enable_multibyte_characters.  */
 13073   insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), true);
 13074 
 13075   return false;
 13076 }
 13077 
 13078 
 13079 /* Clear messages.  CURRENT_P means clear the current message.
 13080    LAST_DISPLAYED_P means clear the message last displayed.  */
 13081 
 13082 void
 13083 clear_message (bool current_p, bool last_displayed_p)
 13084 {
 13085   Lisp_Object preserve = Qnil;
 13086 
 13087   if (current_p)
 13088     {
 13089       if (FUNCTIONP (Vclear_message_function)
 13090           /* FIXME: (bug#63253) Same as for `set-message-function` above.  */
 13091           && !garbage_collection_inhibited)
 13092         {
 13093           specpdl_ref count = SPECPDL_INDEX ();
 13094           specbind (Qinhibit_quit, Qt);
 13095           preserve = safe_call (1, Vclear_message_function);
 13096           unbind_to (count, Qnil);
 13097         }
 13098 
 13099       if (!EQ (preserve, Qdont_clear_message))
 13100         {
 13101           echo_area_buffer[0] = Qnil;
 13102           message_cleared_p = true;
 13103         }
 13104     }
 13105 
 13106   if (last_displayed_p)
 13107     echo_area_buffer[1] = Qnil;
 13108 
 13109   message_buf_print = false;
 13110 }
 13111 
 13112 /* Clear garbaged frames.
 13113 
 13114    This function is used where the old redisplay called
 13115    redraw_garbaged_frames which in turn called redraw_frame which in
 13116    turn called clear_frame.  The call to clear_frame was a source of
 13117    flickering.  I believe a clear_frame is not necessary.  It should
 13118    suffice in the new redisplay to invalidate all current matrices,
 13119    and ensure a complete redisplay of all windows.  */
 13120 
 13121 static void
 13122 clear_garbaged_frames (void)
 13123 {
 13124   if (frame_garbaged)
 13125     {
 13126       Lisp_Object tail, frame;
 13127       struct frame *sf = SELECTED_FRAME ();
 13128 
 13129       FOR_EACH_FRAME (tail, frame)
 13130         {
 13131           struct frame *f = XFRAME (frame);
 13132 
 13133           if (FRAME_REDISPLAY_P (f) && FRAME_GARBAGED_P (f))
 13134             {
 13135               if (f->resized_p
 13136                   /* It makes no sense to redraw a non-selected TTY
 13137                      frame, since that will actually clear the
 13138                      selected frame, and might leave the selected
 13139                      frame with corrupted display, if it happens not
 13140                      to be marked garbaged.  */
 13141                   && !(f != sf && (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))))
 13142                 redraw_frame (f);
 13143               else
 13144                 clear_current_matrices (f);
 13145 
 13146 #ifdef HAVE_WINDOW_SYSTEM
 13147               if (FRAME_WINDOW_P (f)
 13148                   && FRAME_RIF (f)->clear_under_internal_border)
 13149                 FRAME_RIF (f)->clear_under_internal_border (f);
 13150 #endif
 13151               fset_redisplay (f);
 13152               f->garbaged = false;
 13153               f->resized_p = false;
 13154             }
 13155         }
 13156 
 13157       frame_garbaged = false;
 13158     }
 13159 }
 13160 
 13161 
 13162 /* Redisplay the echo area of the selected frame.  If UPDATE_FRAME_P, update
 13163    selected_frame.  */
 13164 
 13165 static void
 13166 echo_area_display (bool update_frame_p)
 13167 {
 13168   Lisp_Object mini_window;
 13169   struct window *w;
 13170   struct frame *f;
 13171   bool window_height_changed_p = false;
 13172   struct frame *sf = SELECTED_FRAME ();
 13173 
 13174   mini_window = FRAME_MINIBUF_WINDOW (sf);
 13175   if (NILP (mini_window))
 13176     return;
 13177 
 13178   w = XWINDOW (mini_window);
 13179   f = XFRAME (WINDOW_FRAME (w));
 13180 
 13181   /* Don't display if frame is invisible or not yet initialized.  */
 13182   if (!FRAME_REDISPLAY_P (f) || !f->glyphs_initialized_p)
 13183     return;
 13184 
 13185 #ifdef HAVE_WINDOW_SYSTEM
 13186   /* When Emacs starts, selected_frame may be the initial terminal
 13187      frame.  If we let this through, a message would be displayed on
 13188      the terminal.  */
 13189   if (FRAME_INITIAL_P (XFRAME (selected_frame)))
 13190     return;
 13191 #endif /* HAVE_WINDOW_SYSTEM */
 13192 
 13193   /* Redraw garbaged frames.  */
 13194   clear_garbaged_frames ();
 13195 
 13196   if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
 13197     {
 13198       echo_area_window = mini_window;
 13199       window_height_changed_p = display_echo_area (w);
 13200       w->must_be_updated_p = true;
 13201 
 13202       /* Update the display, unless called from redisplay_internal.
 13203          Also don't update the screen during redisplay itself.  The
 13204          update will happen at the end of redisplay, and an update
 13205          here could cause confusion.  */
 13206       if (update_frame_p && !redisplaying_p)
 13207         {
 13208           int n = 0;
 13209 
 13210           /* If the display update has been interrupted by pending
 13211              input, update mode lines in the frame.  Due to the
 13212              pending input, it might have been that redisplay hasn't
 13213              been called, so that mode lines above the echo area are
 13214              garbaged.  This looks odd, so we prevent it here.  */
 13215           if (!display_completed)
 13216             {
 13217               n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), false);
 13218 
 13219 #ifdef HAVE_WINDOW_SYSTEM
 13220               if (FRAME_WINDOW_P (f)
 13221                   && FRAME_RIF (f)->clear_under_internal_border)
 13222                 FRAME_RIF (f)->clear_under_internal_border (f);
 13223 #endif
 13224             }
 13225 
 13226           if (window_height_changed_p
 13227               /* Don't do this if Emacs is shutting down.  Redisplay
 13228                  needs to run hooks.  */
 13229               && !NILP (Vrun_hooks))
 13230             {
 13231               /* Must update other windows.  Likewise as in other
 13232                  cases, don't let this update be interrupted by
 13233                  pending input.  */
 13234               specpdl_ref count = SPECPDL_INDEX ();
 13235               specbind (Qredisplay_dont_pause, Qt);
 13236               fset_redisplay (f);
 13237               redisplay_internal ();
 13238               unbind_to (count, Qnil);
 13239             }
 13240           else if (FRAME_WINDOW_P (f) && n == 0)
 13241             {
 13242               /* Window configuration is the same as before.
 13243                  Can do with a display update of the echo area,
 13244                  unless we displayed some mode lines.  */
 13245               update_single_window (w);
 13246               flush_frame (f);
 13247             }
 13248           else
 13249             update_frame (f, true, true);
 13250 
 13251           /* If cursor is in the echo area, make sure that the next
 13252              redisplay displays the minibuffer, so that the cursor will
 13253              be replaced with what the minibuffer wants.  */
 13254           if (cursor_in_echo_area)
 13255             wset_redisplay (XWINDOW (mini_window));
 13256         }
 13257     }
 13258   else if (!EQ (mini_window, selected_window))
 13259     wset_redisplay (XWINDOW (mini_window));
 13260 
 13261   /* Last displayed message is now the current message.  */
 13262   echo_area_buffer[1] = echo_area_buffer[0];
 13263   /* Inform read_char that we're not echoing.  */
 13264   echo_message_buffer = Qnil;
 13265 
 13266   /* Prevent redisplay optimization in redisplay_internal by resetting
 13267      this_line_start_pos.  This is done because the mini-buffer now
 13268      displays the message instead of its buffer text.  */
 13269   if (EQ (mini_window, selected_window))
 13270     CHARPOS (this_line_start_pos) = 0;
 13271 
 13272   if (window_height_changed_p)
 13273     {
 13274       fset_redisplay (f);
 13275 
 13276       /* If window configuration was changed, frames may have been
 13277          marked garbaged.  Clear them or we will experience
 13278          surprises wrt scrolling.
 13279          FIXME: How/why/when?  */
 13280       clear_garbaged_frames ();
 13281     }
 13282 }
 13283 
 13284 /* True if W's buffer was changed but not saved.  */
 13285 
 13286 static bool
 13287 window_buffer_changed (struct window *w)
 13288 {
 13289   struct buffer *b = XBUFFER (w->contents);
 13290 
 13291   eassert (BUFFER_LIVE_P (b));
 13292 
 13293   return (BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star;
 13294 }
 13295 
 13296 /* True if W has %c or %C in its mode line and mode line should be updated.  */
 13297 
 13298 static bool
 13299 mode_line_update_needed (struct window *w)
 13300 {
 13301   return (w->column_number_displayed != -1
 13302           && !(PT == w->last_point && !window_outdated (w))
 13303           && (w->column_number_displayed != current_column ()));
 13304 }
 13305 
 13306 /* True if window start of W is frozen and may not be changed during
 13307    redisplay.  */
 13308 
 13309 static bool
 13310 window_frozen_p (struct window *w)
 13311 {
 13312   if (FRAME_WINDOWS_FROZEN (XFRAME (WINDOW_FRAME (w))))
 13313     {
 13314       Lisp_Object window;
 13315 
 13316       XSETWINDOW (window, w);
 13317       if (MINI_WINDOW_P (w))
 13318         return false;
 13319       else if (EQ (window, selected_window))
 13320         return false;
 13321       else if (MINI_WINDOW_P (XWINDOW (selected_window))
 13322                && EQ (window, Vminibuf_scroll_window))
 13323         /* This special window can't be frozen too.  */
 13324         return false;
 13325       else
 13326         return true;
 13327     }
 13328   return false;
 13329 }
 13330 
 13331 /***********************************************************************
 13332                      Mode Lines and Frame Titles
 13333  ***********************************************************************/
 13334 
 13335 /* A buffer for constructing non-propertized mode-line strings and
 13336    frame titles in it; allocated from the heap in init_xdisp and
 13337    resized as needed in store_mode_line_noprop_char.  */
 13338 
 13339 static char *mode_line_noprop_buf;
 13340 
 13341 /* The buffer's end, and a current output position in it.  */
 13342 
 13343 static char *mode_line_noprop_buf_end;
 13344 static char *mode_line_noprop_ptr;
 13345 
 13346 #define MODE_LINE_NOPROP_LEN(start) \
 13347   ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
 13348 
 13349 static enum {
 13350   MODE_LINE_DISPLAY = 0,
 13351   MODE_LINE_TITLE,
 13352   MODE_LINE_NOPROP,
 13353   MODE_LINE_STRING
 13354 } mode_line_target;
 13355 
 13356 /* Alist that caches the results of :propertize.
 13357    Each element is (PROPERTIZED-STRING . PROPERTY-LIST).  */
 13358 static Lisp_Object mode_line_proptrans_alist;
 13359 
 13360 /* List of strings making up the mode-line.  */
 13361 static Lisp_Object mode_line_string_list;
 13362 
 13363 /* Base face property when building propertized mode line string.  */
 13364 static Lisp_Object mode_line_string_face;
 13365 static Lisp_Object mode_line_string_face_prop;
 13366 
 13367 
 13368 /* Unwind data for mode line strings */
 13369 
 13370 static Lisp_Object Vmode_line_unwind_vector;
 13371 
 13372 static Lisp_Object
 13373 format_mode_line_unwind_data (struct frame *target_frame,
 13374                               struct buffer *obuf,
 13375                               Lisp_Object owin,
 13376                               bool save_proptrans)
 13377 {
 13378   Lisp_Object vector, tmp;
 13379 
 13380   /* Reduce consing by keeping one vector in
 13381      Vwith_echo_area_save_vector.  */
 13382   vector = Vmode_line_unwind_vector;
 13383   Vmode_line_unwind_vector = Qnil;
 13384 
 13385   if (NILP (vector))
 13386     vector = make_nil_vector (12);
 13387 
 13388   ASET (vector, 0, make_fixnum (mode_line_target));
 13389   ASET (vector, 1, make_fixnum (MODE_LINE_NOPROP_LEN (0)));
 13390   ASET (vector, 2, mode_line_string_list);
 13391   ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
 13392   ASET (vector, 4, mode_line_string_face);
 13393   ASET (vector, 5, mode_line_string_face_prop);
 13394 
 13395   if (obuf)
 13396     XSETBUFFER (tmp, obuf);
 13397   else
 13398     tmp = Qnil;
 13399   ASET (vector, 6, tmp);
 13400   ASET (vector, 7, owin);
 13401   if (target_frame)
 13402     {
 13403       Lisp_Object buffer = XWINDOW (target_frame->selected_window)->contents;
 13404       struct buffer *b = XBUFFER (buffer);
 13405       struct buffer *cb = current_buffer;
 13406 
 13407       /* Similarly to `with-selected-window', if the operation selects
 13408          a window on another frame, we must restore that frame's
 13409          selected window, and (for a tty) the top-frame.  */
 13410       ASET (vector, 8, target_frame->selected_window);
 13411       if (FRAME_TERMCAP_P (target_frame))
 13412         ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
 13413 
 13414       /* If we select a window on another frame, make sure that that
 13415          selection does not leave its buffer's point modified when
 13416          unwinding (Bug#32777).  */
 13417       ASET (vector, 10, buffer);
 13418       current_buffer = b;
 13419       ASET (vector, 11, build_marker (current_buffer, PT, PT_BYTE));
 13420       current_buffer = cb;
 13421     }
 13422 
 13423   return vector;
 13424 }
 13425 
 13426 static void
 13427 unwind_format_mode_line (Lisp_Object vector)
 13428 {
 13429   Lisp_Object old_window = AREF (vector, 7);
 13430   Lisp_Object target_frame_window = AREF (vector, 8);
 13431   Lisp_Object old_top_frame = AREF (vector, 9);
 13432 
 13433   mode_line_target = XFIXNUM (AREF (vector, 0));
 13434   mode_line_noprop_ptr = mode_line_noprop_buf + XFIXNUM (AREF (vector, 1));
 13435   mode_line_string_list = AREF (vector, 2);
 13436   if (! EQ (AREF (vector, 3), Qt))
 13437     mode_line_proptrans_alist = AREF (vector, 3);
 13438   mode_line_string_face = AREF (vector, 4);
 13439   mode_line_string_face_prop = AREF (vector, 5);
 13440 
 13441   /* Select window before buffer, since it may change the buffer.  */
 13442   if (WINDOW_LIVE_P (old_window))
 13443     {
 13444       /* If the operation that we are unwinding had selected a window
 13445          on a different frame, reset its frame-selected-window.  For a
 13446          text terminal, reset its top-frame if necessary.  */
 13447       if (WINDOW_LIVE_P (target_frame_window))
 13448         {
 13449           Lisp_Object frame
 13450             = WINDOW_FRAME (XWINDOW (target_frame_window));
 13451 
 13452           if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
 13453             Fselect_window (target_frame_window, Qt);
 13454 
 13455           if (!NILP (old_top_frame) && !EQ (old_top_frame, frame)
 13456               /* This could've been destroyed during the formatting,
 13457                  possibly because the terminal was deleted.  */
 13458               && FRAME_LIVE_P (XFRAME (old_top_frame)))
 13459             Fselect_frame (old_top_frame, Qt);
 13460         }
 13461 
 13462       Fselect_window (old_window, Qt);
 13463 
 13464       /* Restore point of target_frame_window's buffer (Bug#32777).
 13465          But do this only after old_window has been reselected to
 13466          avoid that the window point of target_frame_window moves.  */
 13467       if (WINDOW_LIVE_P (target_frame_window))
 13468         {
 13469           Lisp_Object buffer = AREF (vector, 10);
 13470 
 13471           if (BUFFER_LIVE_P (XBUFFER (buffer)))
 13472             {
 13473               struct buffer *cb = current_buffer;
 13474 
 13475               current_buffer = XBUFFER (buffer);
 13476               set_point_from_marker (AREF (vector, 11));
 13477               ASET (vector, 11, Qnil);
 13478               current_buffer = cb;
 13479             }
 13480         }
 13481     }
 13482 
 13483   if (!NILP (AREF (vector, 6)))
 13484     {
 13485       set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
 13486       ASET (vector, 6, Qnil);
 13487     }
 13488 
 13489   Vmode_line_unwind_vector = vector;
 13490 }
 13491 
 13492 
 13493 /* Store a single character C for the frame title in mode_line_noprop_buf.
 13494    Re-allocate mode_line_noprop_buf if necessary.  */
 13495 
 13496 static void
 13497 store_mode_line_noprop_char (char c)
 13498 {
 13499   /* If output position has reached the end of the allocated buffer,
 13500      increase the buffer's size.  */
 13501   if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
 13502     {
 13503       ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
 13504       ptrdiff_t size = len;
 13505       mode_line_noprop_buf =
 13506         xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
 13507       mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 13508       mode_line_noprop_ptr = mode_line_noprop_buf + len;
 13509     }
 13510 
 13511   *mode_line_noprop_ptr++ = c;
 13512 }
 13513 
 13514 
 13515 /* Store part of a frame title in mode_line_noprop_buf, beginning at
 13516    mode_line_noprop_ptr.  STRING is the string to store.  Do not copy
 13517    characters that yield more columns than PRECISION; PRECISION <= 0
 13518    means copy the whole string.  Pad with spaces until FIELD_WIDTH
 13519    number of characters have been copied; FIELD_WIDTH <= 0 means don't
 13520    pad.  Called from display_mode_element when it is used to build a
 13521    frame title.  */
 13522 
 13523 static int
 13524 store_mode_line_noprop (const char *string, int field_width, int precision)
 13525 {
 13526   const unsigned char *str = (const unsigned char *) string;
 13527   int n = 0;
 13528   ptrdiff_t dummy, nbytes;
 13529 
 13530   /* Copy at most PRECISION chars from STR.  */
 13531   nbytes = strlen (string);
 13532   n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
 13533   while (nbytes--)
 13534     store_mode_line_noprop_char (*str++);
 13535 
 13536   /* Fill up with spaces until FIELD_WIDTH reached.  */
 13537   while (field_width > 0
 13538          && n < field_width)
 13539     {
 13540       store_mode_line_noprop_char (' ');
 13541       ++n;
 13542     }
 13543 
 13544   return n;
 13545 }
 13546 
 13547 /***********************************************************************
 13548                              Frame Titles
 13549  ***********************************************************************/
 13550 
 13551 #ifdef HAVE_WINDOW_SYSTEM
 13552 
 13553 /* Set the title of FRAME, if it has changed.  The title format is
 13554    Vicon_title_format if FRAME is iconified, otherwise it is
 13555    frame_title_format.  */
 13556 
 13557 void
 13558 gui_consider_frame_title (Lisp_Object frame)
 13559 {
 13560   struct frame *f = XFRAME (frame);
 13561   Lisp_Object format_data;
 13562 
 13563   if ((FRAME_WINDOW_P (f)
 13564        || FRAME_MINIBUF_ONLY_P (f)
 13565        || f->explicit_name)
 13566       && !FRAME_TOOLTIP_P (f))
 13567     {
 13568       /* Do we have more than one visible frame on this X display?  */
 13569       Lisp_Object tail, other_frame, fmt;
 13570       ptrdiff_t title_start;
 13571       char *title;
 13572       ptrdiff_t len;
 13573       struct it it;
 13574       specpdl_ref count = SPECPDL_INDEX ();
 13575 
 13576       FOR_EACH_FRAME (tail, other_frame)
 13577         {
 13578           struct frame *tf = XFRAME (other_frame);
 13579 
 13580           if (tf != f
 13581               && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
 13582               && !FRAME_MINIBUF_ONLY_P (tf)
 13583               && !FRAME_PARENT_FRAME (tf)
 13584               && !FRAME_TOOLTIP_P (tf)
 13585               && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
 13586             break;
 13587         }
 13588 
 13589       /* Set global variable indicating that multiple frames exist.  */
 13590       multiple_frames = CONSP (tail);
 13591 
 13592       /* select-frame calls resize_mini_window, which could resize the
 13593          mini-window and by that undo the effect of this redisplay
 13594          cycle wrt minibuffer and echo-area display.  Binding
 13595          inhibit-redisplay to t makes the call to resize_mini_window a
 13596          no-op, thus avoiding the adverse side effects.  */
 13597 
 13598       /* The following was moved before the record_unwind_protect form
 13599          below to inhibit redisplay also when restoring the selected
 13600          window/frame: This avoids that resize_mini_window sizes back
 13601          the minibuffer window of a temporarily selected frame.  See
 13602          Bug#34317.  */
 13603       specbind (Qinhibit_redisplay, Qt);
 13604 
 13605       /* Switch to the buffer of selected window of the frame.  Set up
 13606          mode_line_target so that display_mode_element will output
 13607          into mode_line_noprop_buf; then display the title.  Save the
 13608          original frame and selected window, and possibly the topmost
 13609          frame of the tty (for tty frames) into a vector; it will be
 13610          restored later.  */
 13611 
 13612       format_data = format_mode_line_unwind_data (f, current_buffer,
 13613                                                   selected_window,
 13614                                                   false);
 13615       record_unwind_protect (unwind_format_mode_line, format_data);
 13616 
 13617       Fselect_window (f->selected_window, Qt);
 13618       set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->contents));
 13619       fmt = (FRAME_ICONIFIED_P (f) && !EQ (Vicon_title_format, Qt)
 13620              ? Vicon_title_format : Vframe_title_format);
 13621 
 13622       mode_line_target = MODE_LINE_TITLE;
 13623       title_start = MODE_LINE_NOPROP_LEN (0);
 13624       init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
 13625                      NULL, DEFAULT_FACE_ID);
 13626       display_mode_element (&it, 0, -1, -1, fmt, Qnil, false);
 13627       len = MODE_LINE_NOPROP_LEN (title_start);
 13628       title = mode_line_noprop_buf + title_start;
 13629       /* Make sure that any raw bytes in the title are properly
 13630          represented by their multibyte sequences.  */
 13631       ptrdiff_t nchars = 0;
 13632       len = str_as_multibyte ((unsigned char *) title,
 13633                               mode_line_noprop_buf_end - title,
 13634                               len, &nchars);
 13635       unbind_to (count, Qnil);
 13636 
 13637       /* Set the title only if it's changed.  This avoids consing in
 13638          the common case where it hasn't.  (If it turns out that we've
 13639          already wasted too much time by walking through the list with
 13640          display_mode_element, then we might need to optimize at a
 13641          higher level than this.)  */
 13642       if ((! STRINGP (f->name)
 13643            || SBYTES (f->name) != len
 13644            || memcmp (title, SDATA (f->name), len) != 0)
 13645           && FRAME_TERMINAL (f)->implicit_set_name_hook)
 13646         {
 13647           Lisp_Object title_string = make_multibyte_string (title, nchars, len);
 13648           FRAME_TERMINAL (f)->implicit_set_name_hook (f, title_string, Qnil);
 13649         }
 13650     }
 13651 }
 13652 
 13653 #endif /* not HAVE_WINDOW_SYSTEM */
 13654 
 13655 
 13656 /***********************************************************************
 13657                               Menu Bars
 13658  ***********************************************************************/
 13659 
 13660 /* True if we will not redisplay all visible windows.  */
 13661 #define REDISPLAY_SOME_P()                              \
 13662   ((windows_or_buffers_changed == 0                     \
 13663     || windows_or_buffers_changed == REDISPLAY_SOME)    \
 13664    && (update_mode_lines == 0                           \
 13665        || update_mode_lines == REDISPLAY_SOME))
 13666 
 13667 static bool
 13668 needs_no_redisplay (struct window *w)
 13669 {
 13670   struct buffer *buffer = XBUFFER (w->contents);
 13671   struct frame *f = XFRAME (w->frame);
 13672   return (REDISPLAY_SOME_P ()
 13673           && !w->redisplay
 13674           && !w->update_mode_line
 13675           && !f->face_change
 13676           && !f->redisplay
 13677           && !buffer->text->redisplay
 13678           && window_point (w) == w->last_point);
 13679 }
 13680 
 13681 /* Prepare for redisplay by updating menu-bar item lists when
 13682    appropriate.  This can call eval.  */
 13683 
 13684 static void
 13685 prepare_menu_bars (void)
 13686 {
 13687   bool all_windows = windows_or_buffers_changed || update_mode_lines;
 13688   bool some_windows = REDISPLAY_SOME_P ();
 13689 
 13690   if (FUNCTIONP (Vpre_redisplay_function))
 13691     {
 13692       Lisp_Object windows = all_windows ? Qt : Qnil;
 13693       if (all_windows && some_windows)
 13694         {
 13695           Lisp_Object ws = window_list ();
 13696           for (windows = Qnil; CONSP (ws); ws = XCDR (ws))
 13697             {
 13698               Lisp_Object this = XCAR (ws);
 13699               struct window *w = XWINDOW (this);
 13700               /* Cf. conditions for redisplaying a window at the
 13701                  beginning of redisplay_window.  */
 13702               if (!needs_no_redisplay (w))
 13703                 windows = Fcons (this, windows);
 13704             }
 13705         }
 13706       safe__call1 (true, Vpre_redisplay_function, windows);
 13707     }
 13708 
 13709   /* Update all frame titles based on their buffer names, etc.  We do
 13710      this before the menu bars so that the buffer-menu will show the
 13711      up-to-date frame titles.  */
 13712 #ifdef HAVE_WINDOW_SYSTEM
 13713   if (all_windows)
 13714     {
 13715       Lisp_Object tail, frame;
 13716 
 13717       FOR_EACH_FRAME (tail, frame)
 13718         {
 13719           struct frame *f = XFRAME (frame);
 13720           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13721           if (some_windows
 13722               && !f->redisplay
 13723               && !w->redisplay
 13724               && !XBUFFER (w->contents)->text->redisplay)
 13725             continue;
 13726 
 13727           if (!FRAME_TOOLTIP_P (f)
 13728               && (FRAME_ICONIFIED_P (f)
 13729                   || FRAME_VISIBLE_P (f) == 1
 13730                   /* Exclude TTY frames that are obscured because they
 13731                      are not the top frame on their console.  This is
 13732                      because gui_consider_frame_title actually switches
 13733                      to the frame, which for TTY frames means it is
 13734                      marked as garbaged, and will be completely
 13735                      redrawn on the next redisplay cycle.  This causes
 13736                      TTY frames to be completely redrawn, when there
 13737                      are more than one of them, even though nothing
 13738                      should be changed on display.  */
 13739                   || (FRAME_REDISPLAY_P (f) && FRAME_WINDOW_P (f))))
 13740             gui_consider_frame_title (frame);
 13741         }
 13742     }
 13743 #endif /* HAVE_WINDOW_SYSTEM */
 13744 
 13745   /* Update the menu bar item lists, if appropriate.  This has to be
 13746      done before any actual redisplay or generation of display lines.  */
 13747 
 13748   if (all_windows)
 13749     {
 13750       Lisp_Object tail, frame;
 13751       specpdl_ref count = SPECPDL_INDEX ();
 13752       /* True means that update_menu_bar has run its hooks
 13753          so any further calls to update_menu_bar shouldn't do so again.  */
 13754       bool menu_bar_hooks_run = false;
 13755 
 13756       record_unwind_save_match_data ();
 13757 
 13758       FOR_EACH_FRAME (tail, frame)
 13759         {
 13760           struct frame *f = XFRAME (frame);
 13761           struct window *w = XWINDOW (FRAME_SELECTED_WINDOW (f));
 13762 
 13763           /* Ignore tooltip frame.  */
 13764           if (FRAME_TOOLTIP_P (f))
 13765             continue;
 13766 
 13767           if (some_windows
 13768               && !f->redisplay
 13769               && !w->redisplay
 13770               && !XBUFFER (w->contents)->text->redisplay)
 13771             continue;
 13772 
 13773           if (!FRAME_PARENT_FRAME (f))
 13774             menu_bar_hooks_run = update_menu_bar (f, false, menu_bar_hooks_run);
 13775 
 13776           update_tab_bar (f, false);
 13777 #ifdef HAVE_WINDOW_SYSTEM
 13778           update_tool_bar (f, false);
 13779 #endif
 13780         }
 13781 
 13782       unbind_to (count, Qnil);
 13783     }
 13784   else
 13785     {
 13786       struct frame *sf = SELECTED_FRAME ();
 13787 
 13788       if (!FRAME_PARENT_FRAME (sf))
 13789         update_menu_bar (sf, true, false);
 13790 
 13791       update_tab_bar (sf, true);
 13792 #ifdef HAVE_WINDOW_SYSTEM
 13793       update_tool_bar (sf, true);
 13794 #endif
 13795     }
 13796 }
 13797 
 13798 
 13799 /* Update the menu bar item list for frame F.  This has to be done
 13800    before we start to fill in any display lines, because it can call
 13801    eval.
 13802 
 13803    If SAVE_MATCH_DATA, we must save and restore it here.
 13804 
 13805    If HOOKS_RUN, a previous call to update_menu_bar
 13806    already ran the menu bar hooks for this redisplay, so there
 13807    is no need to run them again.  The return value is the
 13808    updated value of this flag, to pass to the next call.  */
 13809 
 13810 static bool
 13811 update_menu_bar (struct frame *f, bool save_match_data, bool hooks_run)
 13812 {
 13813   Lisp_Object window;
 13814   struct window *w;
 13815 
 13816   /* If called recursively during a menu update, do nothing.  This can
 13817      happen when, for instance, an activate-menubar-hook causes a
 13818      redisplay.  */
 13819   if (inhibit_menubar_update)
 13820     return hooks_run;
 13821 
 13822   window = FRAME_SELECTED_WINDOW (f);
 13823   w = XWINDOW (window);
 13824 
 13825   if (FRAME_WINDOW_P (f)
 13826       ?
 13827 #ifdef HAVE_EXT_MENU_BAR
 13828       FRAME_EXTERNAL_MENU_BAR (f)
 13829 #else
 13830       FRAME_MENU_BAR_LINES (f) > 0
 13831 #endif
 13832       : FRAME_MENU_BAR_LINES (f) > 0)
 13833     {
 13834       /* If the user has switched buffers or windows, we need to
 13835          recompute to reflect the new bindings.  But we'll
 13836          recompute when update_mode_lines is set too; that means
 13837          that people can use force-mode-line-update to request
 13838          that the menu bar be recomputed.  The adverse effect on
 13839          the rest of the redisplay algorithm is about the same as
 13840          windows_or_buffers_changed anyway.  */
 13841       if (windows_or_buffers_changed
 13842           /* This used to test w->update_mode_line, but we believe
 13843              there is no need to recompute the menu in that case.  */
 13844           || update_mode_lines
 13845           || window_buffer_changed (w))
 13846         {
 13847           struct buffer *prev = current_buffer;
 13848           specpdl_ref count = SPECPDL_INDEX ();
 13849 
 13850           specbind (Qinhibit_menubar_update, Qt);
 13851 
 13852           set_buffer_internal_1 (XBUFFER (w->contents));
 13853           if (save_match_data)
 13854             record_unwind_save_match_data ();
 13855           if (NILP (Voverriding_local_map_menu_flag))
 13856             {
 13857               specbind (Qoverriding_terminal_local_map, Qnil);
 13858               specbind (Qoverriding_local_map, Qnil);
 13859             }
 13860 
 13861           if (!hooks_run)
 13862             {
 13863               /* Run the Lucid hook.  */
 13864               safe_run_hooks (Qactivate_menubar_hook);
 13865 
 13866               /* If it has changed current-menubar from previous value,
 13867                  really recompute the menu-bar from the value.  */
 13868               safe_run_hooks (Qmenu_bar_update_hook);
 13869 
 13870               hooks_run = true;
 13871             }
 13872 
 13873           XSETFRAME (Vmenu_updating_frame, f);
 13874           fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
 13875 
 13876           /* Redisplay the menu bar in case we changed it.  */
 13877 #ifdef HAVE_EXT_MENU_BAR
 13878           if (FRAME_WINDOW_P (f))
 13879             {
 13880 #if defined (HAVE_NS)
 13881               /* All frames on Mac OS share the same menubar.  So only
 13882                  the selected frame should be allowed to set it.  */
 13883               if (f == SELECTED_FRAME ())
 13884 #endif
 13885                 set_frame_menubar (f, false);
 13886             }
 13887           else
 13888             /* On a terminal screen, the menu bar is an ordinary screen
 13889                line, and this makes it get updated.  */
 13890             w->update_mode_line = true;
 13891 #else /* ! (HAVE_EXT_MENU_BAR) */
 13892           /* In the non-toolkit version, the menu bar is an ordinary screen
 13893              line, and this makes it get updated.  */
 13894           w->update_mode_line = true;
 13895 #endif /* HAVE_EXT_MENU_BAR */
 13896 
 13897           unbind_to (count, Qnil);
 13898           set_buffer_internal_1 (prev);
 13899         }
 13900     }
 13901 
 13902   return hooks_run;
 13903 }
 13904 
 13905 
 13906 
 13907 /***********************************************************************
 13908                                Tab-bars
 13909  ***********************************************************************/
 13910 
 13911 /* Restore WINDOW as the selected window and its frame as the selected
 13912    frame.  If WINDOW is dead but the selected frame is live, make the
 13913    latter's selected window the selected window.  If both, WINDOW and
 13914    the selected frame, are dead, assign selected frame and window from
 13915    some arbitrary live frame.  Abort if no such frame can be found.  */
 13916 static void
 13917 restore_selected_window (Lisp_Object window)
 13918 {
 13919   if (WINDOW_LIVE_P (window))
 13920     /* If WINDOW is live, make it the selected window and its frame's
 13921        selected window and set the selected frame to its frame.  */
 13922     {
 13923       selected_window = window;
 13924       selected_frame = XWINDOW (window)->frame;
 13925       FRAME_SELECTED_WINDOW (XFRAME (selected_frame)) = window;
 13926     }
 13927   else if (FRAMEP (selected_frame) && FRAME_LIVE_P (XFRAME (selected_frame)))
 13928     /* If WINDOW is dead but the selected frame is still live, make the
 13929        latter's selected window the selected one.  */
 13930     selected_window = FRAME_SELECTED_WINDOW (XFRAME (selected_frame));
 13931   else
 13932     /* If WINDOW and the selected frame are dead, choose some live,
 13933        non-child and non-tooltip frame as the new selected frame and
 13934        make its selected window the selected window.  */
 13935     {
 13936       Lisp_Object tail;
 13937       Lisp_Object frame UNINIT;
 13938 
 13939       FOR_EACH_FRAME (tail, frame)
 13940         {
 13941           struct frame *f = XFRAME (frame);
 13942 
 13943           if (!FRAME_PARENT_FRAME (f) && !FRAME_TOOLTIP_P (f))
 13944             {
 13945               selected_frame = frame;
 13946               selected_window = FRAME_SELECTED_WINDOW (f);
 13947 
 13948               return;
 13949             }
 13950         }
 13951 
 13952       /* Abort if we cannot find a live frame.  */
 13953       emacs_abort ();
 13954     }
 13955 }
 13956 
 13957 /* Restore WINDOW, if live, as its frame's selected window.  */
 13958 static void
 13959 restore_frame_selected_window (Lisp_Object window)
 13960 {
 13961   if (WINDOW_LIVE_P (window))
 13962     /* If WINDOW is live, make it its frame's selected window.  If that
 13963        frame is the selected frame, make WINDOW the selected window as
 13964        well.  */
 13965     {
 13966       Lisp_Object frame = XWINDOW (window)->frame;
 13967 
 13968       FRAME_SELECTED_WINDOW (XFRAME (frame)) = window;
 13969       if (EQ (frame, selected_frame))
 13970         selected_window = window;
 13971     }
 13972 }
 13973 
 13974 /* Update the tab-bar item list for frame F.  This has to be done
 13975    before we start to fill in any display lines.  Called from
 13976    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 13977    and restore it here.  */
 13978 
 13979 static void
 13980 update_tab_bar (struct frame *f, bool save_match_data)
 13981 {
 13982   bool do_update = false;
 13983 
 13984 #ifdef HAVE_WINDOW_SYSTEM
 13985   if (FRAME_WINDOW_P (f) && WINDOWP (f->tab_bar_window)) {
 13986     if (WINDOW_TOTAL_LINES (XWINDOW (f->tab_bar_window)) > 0)
 13987       do_update = true;
 13988   }
 13989   else
 13990 #endif
 13991   if (FRAME_TAB_BAR_LINES (f) > 0)
 13992     do_update = true;
 13993 
 13994   if (do_update)
 13995     {
 13996       Lisp_Object window;
 13997       struct window *w;
 13998 
 13999       window = FRAME_SELECTED_WINDOW (f);
 14000       w = XWINDOW (window);
 14001 
 14002       /* If the user has switched buffers or windows, we need to
 14003          recompute to reflect the new bindings.  But we'll
 14004          recompute when update_mode_lines is set too; that means
 14005          that people can use force-mode-line-update to request
 14006          that the menu bar be recomputed.  The adverse effect on
 14007          the rest of the redisplay algorithm is about the same as
 14008          windows_or_buffers_changed anyway.  */
 14009       if (windows_or_buffers_changed
 14010           || w->update_mode_line
 14011           || update_mode_lines
 14012           || window_buffer_changed (w))
 14013         {
 14014           struct buffer *prev = current_buffer;
 14015           specpdl_ref count = SPECPDL_INDEX ();
 14016           Lisp_Object new_tab_bar;
 14017           int new_n_tab_bar;
 14018 
 14019           /* Set current_buffer to the buffer of the selected
 14020              window of the frame, so that we get the right local
 14021              keymaps.  */
 14022           set_buffer_internal_1 (XBUFFER (w->contents));
 14023 
 14024           /* Save match data, if we must.  */
 14025           if (save_match_data)
 14026             record_unwind_save_match_data ();
 14027 
 14028           /* Make sure that we don't accidentally use bogus keymaps.  */
 14029           if (NILP (Voverriding_local_map_menu_flag))
 14030             {
 14031               specbind (Qoverriding_terminal_local_map, Qnil);
 14032               specbind (Qoverriding_local_map, Qnil);
 14033             }
 14034 
 14035           /* We must temporarily set the selected frame to this frame
 14036              before calling tab_bar_items, because the calculation of
 14037              the tab-bar keymap uses the selected frame (see
 14038              `tab-bar-make-keymap' in tab-bar.el).  */
 14039           eassert (EQ (selected_window,
 14040                        /* Since we only explicitly preserve selected_frame,
 14041                           check that selected_window would be redundant.  */
 14042                        XFRAME (selected_frame)->selected_window));
 14043 #ifdef HAVE_WINDOW_SYSTEM
 14044           Lisp_Object frame;
 14045           record_unwind_protect (restore_selected_window, selected_window);
 14046           XSETFRAME (frame, f);
 14047           selected_frame = frame;
 14048           selected_window = FRAME_SELECTED_WINDOW (f);
 14049 #endif
 14050 
 14051           /* Build desired tab-bar items from keymaps.  */
 14052           new_tab_bar
 14053             = tab_bar_items (Fcopy_sequence (f->tab_bar_items),
 14054                               &new_n_tab_bar);
 14055 
 14056           /* Redisplay the tab-bar if we changed it.  */
 14057           if (new_n_tab_bar != f->n_tab_bar_items
 14058               || NILP (Fequal (new_tab_bar, f->tab_bar_items)))
 14059             {
 14060               /* Redisplay that happens asynchronously due to an expose event
 14061                  may access f->tab_bar_items.  Make sure we update both
 14062                  variables within BLOCK_INPUT so no such event interrupts.  */
 14063               block_input ();
 14064               fset_tab_bar_items (f, new_tab_bar);
 14065               f->n_tab_bar_items = new_n_tab_bar;
 14066               w->update_mode_line = true;
 14067               unblock_input ();
 14068             }
 14069 
 14070           unbind_to (count, Qnil);
 14071           set_buffer_internal_1 (prev);
 14072         }
 14073     }
 14074 }
 14075 
 14076 /* Redisplay the tab bar in the frame for window W.
 14077 
 14078    The tab bar of X frames that don't have X toolkit support is
 14079    displayed in a special window W->frame->tab_bar_window.
 14080 
 14081    The tab bar of terminal frames is treated specially as far as
 14082    glyph matrices are concerned.  Tab bar lines are not part of
 14083    windows, so the update is done directly on the frame matrix rows
 14084    for the tab bar.  */
 14085 
 14086 static void
 14087 display_tab_bar (struct window *w)
 14088 {
 14089   struct frame *f = XFRAME (WINDOW_FRAME (w));
 14090   struct it it;
 14091   Lisp_Object items;
 14092   int i;
 14093 
 14094   /* Don't do all this for graphical frames.  */
 14095 #ifdef HAVE_NTGUI
 14096   if (FRAME_W32_P (f))
 14097     return;
 14098 #endif
 14099 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14100   if (FRAME_X_P (f))
 14101     return;
 14102 #endif
 14103 
 14104 #ifdef HAVE_NS
 14105   if (FRAME_NS_P (f))
 14106     return;
 14107 #endif /* HAVE_NS */
 14108 
 14109 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 14110   eassert (!FRAME_WINDOW_P (f));
 14111   init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14112                  + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14113                  TAB_BAR_FACE_ID);
 14114   it.first_visible_x = 0;
 14115   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14116 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 14117   if (FRAME_WINDOW_P (f))
 14118     {
 14119       /* Tab bar lines are displayed in the desired matrix of the
 14120          dummy window tab_bar_window.  */
 14121       struct window *tab_w;
 14122       tab_w = XWINDOW (f->tab_bar_window);
 14123       init_iterator (&it, tab_w, -1, -1, tab_w->desired_matrix->rows,
 14124                      TAB_BAR_FACE_ID);
 14125       it.first_visible_x = 0;
 14126       it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 14127     }
 14128   else
 14129 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 14130     {
 14131       /* This is a TTY frame, i.e. character hpos/vpos are used as
 14132          pixel x/y.  */
 14133       init_iterator (&it, w, -1, -1, f->desired_matrix->rows
 14134                      + (FRAME_MENU_BAR_LINES (f) > 0 ? 1 : 0),
 14135                      TAB_BAR_FACE_ID);
 14136       it.first_visible_x = 0;
 14137       it.last_visible_x = FRAME_COLS (f);
 14138     }
 14139 
 14140   /* FIXME: This should be controlled by a user option.  See the
 14141      comments in redisplay_tool_bar and display_mode_line about
 14142      this.  */
 14143   it.paragraph_embedding = L2R;
 14144 
 14145   /* Clear all rows of the tab bar.  */
 14146   for (i = 0; i < FRAME_TAB_BAR_LINES (f); ++i)
 14147     {
 14148       struct glyph_row *row = it.glyph_row + i;
 14149       clear_glyph_row (row);
 14150       row->enabled_p = true;
 14151       row->full_width_p = true;
 14152       row->reversed_p = false;
 14153     }
 14154 
 14155   /* Display all items of the tab bar.  */
 14156   items = it.f->tab_bar_items;
 14157   int j;
 14158   for (i = 0, j = 0; i < it.f->n_tab_bar_items; ++i, j += TAB_BAR_ITEM_NSLOTS)
 14159     {
 14160       Lisp_Object string = AREF (items, j + TAB_BAR_ITEM_CAPTION);
 14161 
 14162       /* Stop at nil string.  */
 14163       if (NILP (string))
 14164         break;
 14165 
 14166       if (it.current_x < it.last_visible_x)
 14167         display_string (NULL, string, Qnil, 0, 0, &it,
 14168                         SCHARS (string), 0, 0, STRING_MULTIBYTE (string));
 14169     }
 14170 
 14171   /* Fill out the line with spaces.  */
 14172   if (it.current_x < it.last_visible_x)
 14173     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 14174 
 14175   /* Compute the total height of the lines.  */
 14176   compute_line_metrics (&it);
 14177 }
 14178 
 14179 #ifdef HAVE_WINDOW_SYSTEM
 14180 
 14181 /* Set F->desired_tab_bar_string to a Lisp string representing frame
 14182    F's desired tab-bar contents.  F->tab_bar_items must have
 14183    been set up previously by calling prepare_menu_bars.  */
 14184 
 14185 static void
 14186 build_desired_tab_bar_string (struct frame *f)
 14187 {
 14188   int i;
 14189   Lisp_Object caption;
 14190 
 14191   caption = Qnil;
 14192 
 14193   /* Prepare F->desired_tab_bar_string.  Make a new string.  */
 14194   fset_desired_tab_bar_string (f, build_string (""));
 14195 
 14196   /* Put a `display' property on the string for the captions to display,
 14197      put a `menu_item' property on tab-bar items with a value that
 14198      is the index of the item in F's tab-bar item vector.  */
 14199   for (i = 0; i < f->n_tab_bar_items; ++i)
 14200     {
 14201 #define PROP(IDX) \
 14202   AREF (f->tab_bar_items, i * TAB_BAR_ITEM_NSLOTS + (IDX))
 14203 
 14204       caption = Fcopy_sequence (PROP (TAB_BAR_ITEM_CAPTION));
 14205 
 14206       /* Put a `display' text property on the string for the caption to
 14207          display.  Put a `menu-item' property on the string that gives
 14208          the start of this item's properties in the tab-bar items
 14209          vector.  */
 14210       AUTO_LIST2 (props, Qmenu_item, make_fixnum (i * TAB_BAR_ITEM_NSLOTS));
 14211 
 14212       Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14213                             props, caption);
 14214 
 14215       f->desired_tab_bar_string =
 14216         concat2 (f->desired_tab_bar_string, caption);
 14217 
 14218 #undef PROP
 14219     }
 14220 }
 14221 
 14222 
 14223 /* Display one line of the tab-bar of frame IT->f.
 14224 
 14225    HEIGHT specifies the desired height of the tab-bar line.
 14226    If the actual height of the glyph row is less than HEIGHT, the
 14227    row's height is increased to HEIGHT, and the icons are centered
 14228    vertically in the new height.
 14229 
 14230    If HEIGHT is -1, we are counting needed tab-bar lines, so don't
 14231    count a final empty row in case the tab-bar width exactly matches
 14232    the window width.
 14233 */
 14234 
 14235 static void
 14236 display_tab_bar_line (struct it *it, int height)
 14237 {
 14238   struct glyph_row *row = it->glyph_row;
 14239   int max_x = it->last_visible_x;
 14240   struct glyph *last;
 14241 
 14242   /* Don't extend on a previously drawn tab bar items (Bug#16058).  */
 14243   clear_glyph_row (row);
 14244   row->enabled_p = true;
 14245   row->y = it->current_y;
 14246 
 14247   /* Note that this isn't made use of if the face hasn't a box,
 14248      so there's no need to check the face here.  */
 14249   it->start_of_box_run_p = true;
 14250 
 14251   bool enough = false;
 14252   while (it->current_x < max_x)
 14253     {
 14254       int x, n_glyphs_before, i, nglyphs;
 14255       struct it it_before;
 14256 
 14257       /* Get the next display element.  */
 14258       if (!get_next_display_element (it))
 14259         {
 14260           /* Don't count empty row if we are counting needed tab-bar lines.  */
 14261           if (height < 0 && !it->hpos)
 14262             return;
 14263           break;
 14264         }
 14265 
 14266       /* Produce glyphs.  */
 14267       n_glyphs_before = row->used[TEXT_AREA];
 14268       it_before = *it;
 14269 
 14270       PRODUCE_GLYPHS (it);
 14271 
 14272       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 14273       i = 0;
 14274       x = it_before.current_x;
 14275       while (i < nglyphs)
 14276         {
 14277           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 14278 
 14279           if (x + glyph->pixel_width > max_x)
 14280             {
 14281               /* Glyph doesn't fit on line.  Backtrack.  */
 14282               row->used[TEXT_AREA] = n_glyphs_before;
 14283               *it = it_before;
 14284               /* If this is the only glyph on this line, it will never fit on the
 14285                  tab-bar, so skip it.  But ensure there is at least one glyph,
 14286                  so we don't accidentally disable the tab-bar.  */
 14287               if (n_glyphs_before == 0
 14288                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 14289                 break;
 14290               goto out;
 14291             }
 14292 
 14293           ++it->hpos;
 14294           x += glyph->pixel_width;
 14295           ++i;
 14296         }
 14297 
 14298       enough = ITERATOR_AT_END_OF_LINE_P (it);
 14299       set_iterator_to_next (it, true);
 14300 
 14301       /* Stop at line end.  */
 14302       if (enough)
 14303         break;
 14304     }
 14305 
 14306  out:;
 14307 
 14308   row->displays_text_p = row->used[TEXT_AREA] != 0;
 14309 
 14310   /* Use default face for the border below the tab bar.
 14311 
 14312      FIXME: When auto-resize-tab-bars is grow-only, there is
 14313      no additional border below the possibly empty tab-bar lines.
 14314      So to make the extra empty lines look "normal", we have to
 14315      use the tab-bar face for the border too.  */
 14316   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14317       && !EQ (Vauto_resize_tab_bars, Qgrow_only))
 14318     it->face_id = DEFAULT_FACE_ID;
 14319 
 14320   extend_face_to_end_of_line (it);
 14321   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 14322   last->right_box_line_p = true;
 14323   if (last == row->glyphs[TEXT_AREA])
 14324     last->left_box_line_p = true;
 14325 
 14326   /* Make line the desired height and center it vertically.  */
 14327   if ((height -= it->max_ascent + it->max_descent) > 0)
 14328     {
 14329       /* Don't add more than one line height.  */
 14330       height %= FRAME_LINE_HEIGHT (it->f);
 14331       it->max_ascent += height / 2;
 14332       it->max_descent += (height + 1) / 2;
 14333     }
 14334 
 14335   compute_line_metrics (it);
 14336 
 14337   /* If line is empty, make it occupy the rest of the tab-bar.  */
 14338   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 14339     {
 14340       row->height = row->phys_height = it->last_visible_y - row->y;
 14341       row->visible_height = row->height;
 14342       row->ascent = row->phys_ascent = 0;
 14343       row->extra_line_spacing = 0;
 14344     }
 14345 
 14346   row->full_width_p = true;
 14347   row->continued_p = false;
 14348   row->truncated_on_left_p = false;
 14349   row->truncated_on_right_p = false;
 14350 
 14351   it->current_x = it->hpos = 0;
 14352   it->current_y += row->height;
 14353   ++it->vpos;
 14354   ++it->glyph_row;
 14355 }
 14356 
 14357 
 14358 /* Value is the number of pixels needed to make all tab-bar items of
 14359    frame F visible.  The actual number of glyph rows needed is
 14360    returned in *N_ROWS if non-NULL.  */
 14361 static int
 14362 tab_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 14363 {
 14364   struct window *w = XWINDOW (f->tab_bar_window);
 14365   struct it it;
 14366   /* tab_bar_height is called from redisplay_tab_bar after building
 14367      the desired matrix, so use (unused) mode-line row as temporary row to
 14368      avoid destroying the first tab-bar row.  */
 14369   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 14370 
 14371   /* Initialize an iterator for iteration over
 14372      F->desired_tab_bar_string in the tab-bar window of frame F.  */
 14373   init_iterator (&it, w, -1, -1, temp_row, TAB_BAR_FACE_ID);
 14374   temp_row->reversed_p = false;
 14375   it.first_visible_x = 0;
 14376   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14377   reseat_to_string (&it, NULL, f->desired_tab_bar_string,
 14378                     0, 0, 0, STRING_MULTIBYTE (f->desired_tab_bar_string));
 14379   it.paragraph_embedding = L2R;
 14380 
 14381   clear_glyph_row (temp_row);
 14382   while (!ITERATOR_AT_END_P (&it))
 14383     {
 14384       it.glyph_row = temp_row;
 14385       display_tab_bar_line (&it, -1);
 14386     }
 14387   clear_glyph_row (temp_row);
 14388 
 14389   /* f->n_tab_bar_rows == 0 means "unknown"; -1 means no tab-bar.  */
 14390   if (n_rows)
 14391     *n_rows = it.vpos > 0 ? it.vpos : -1;
 14392 
 14393   if (pixelwise)
 14394     return it.current_y;
 14395   else
 14396     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 14397 }
 14398 
 14399 DEFUN ("tab-bar-height", Ftab_bar_height, Stab_bar_height,
 14400        0, 2, 0,
 14401        doc: /* Return the number of lines occupied by the tab bar of FRAME.
 14402 If FRAME is nil or omitted, use the selected frame.  Optional argument
 14403 PIXELWISE non-nil means return the height of the tab bar in pixels.  */)
 14404   (Lisp_Object frame, Lisp_Object pixelwise)
 14405 {
 14406   int height = 0;
 14407 
 14408   struct frame *f = decode_any_frame (frame);
 14409 
 14410   if (WINDOWP (f->tab_bar_window)
 14411       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tab_bar_window)) > 0)
 14412     {
 14413       update_tab_bar (f, true);
 14414       if (f->n_tab_bar_items)
 14415         {
 14416           build_desired_tab_bar_string (f);
 14417           height = tab_bar_height (f, NULL, !NILP (pixelwise));
 14418         }
 14419     }
 14420 
 14421   return make_fixnum (height);
 14422 }
 14423 
 14424 
 14425 /* Display the tab-bar of frame F.  Value is true if tab-bar's
 14426    height should be changed.  */
 14427 static bool
 14428 redisplay_tab_bar (struct frame *f)
 14429 {
 14430   struct window *w;
 14431   struct it it;
 14432   struct glyph_row *row;
 14433 
 14434   f->tab_bar_redisplayed = true;
 14435 
 14436   /* If frame hasn't a tab-bar window or if it is zero-height, don't
 14437      do anything.  This means you must start with tab-bar-lines
 14438      non-zero to get the auto-sizing effect.  Or in other words, you
 14439      can turn off tab-bars by specifying tab-bar-lines zero.  */
 14440   if (!WINDOWP (f->tab_bar_window)
 14441       || (w = XWINDOW (f->tab_bar_window),
 14442           WINDOW_TOTAL_LINES (w) == 0))
 14443     {
 14444       /* Even if we do not display a tab bar initially, still pretend
 14445          that we have resized it.  This avoids that a later activation
 14446          of the tab bar resizes the frame, despite of the fact that the
 14447          setting of 'frame-inhibit-implied-resize' should inhibit it
 14448          (Bug#52986).  */
 14449       f->tab_bar_resized = true;
 14450 
 14451       return false;
 14452     }
 14453 
 14454   /* Build a string that represents the contents of the tab-bar.  */
 14455   build_desired_tab_bar_string (f);
 14456 
 14457   int new_nrows;
 14458   int new_height = tab_bar_height (f, &new_nrows, true);
 14459 
 14460   if (f->n_tab_bar_rows == 0)
 14461     {
 14462       f->n_tab_bar_rows = new_nrows;
 14463       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 14464         frame_default_tab_bar_height = new_height;
 14465     }
 14466 
 14467   /* If new_height or new_nrows indicate that we need to enlarge or
 14468      shrink the tab-bar window, we can return right away.  */
 14469   if (new_nrows > f->n_tab_bar_rows
 14470       || (EQ (Vauto_resize_tab_bars, Qgrow_only)
 14471           && !f->minimize_tab_bar_window_p
 14472           && new_height > WINDOW_PIXEL_HEIGHT (w))
 14473       || (! EQ (Vauto_resize_tab_bars, Qgrow_only)
 14474           && new_height < WINDOW_PIXEL_HEIGHT (w)))
 14475     {
 14476       if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14477         FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14478       if (new_nrows != f->n_tab_bar_rows)
 14479         f->n_tab_bar_rows = new_nrows;
 14480       clear_glyph_matrix (w->desired_matrix);
 14481       f->fonts_changed = true;
 14482       return true;
 14483     }
 14484 
 14485   /* Set up an iterator for the tab-bar window.  */
 14486   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TAB_BAR_FACE_ID);
 14487   it.first_visible_x = 0;
 14488   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 14489   row = it.glyph_row;
 14490   row->reversed_p = false;
 14491   reseat_to_string (&it, NULL, f->desired_tab_bar_string, 0, 0, 0,
 14492                     STRING_MULTIBYTE (f->desired_tab_bar_string));
 14493   /* FIXME: This should be controlled by a user option.  But it
 14494      doesn't make sense to have an R2L tab bar if the menu bar cannot
 14495      be drawn also R2L, and making the menu bar R2L is tricky due
 14496      tabkit-specific code that implements it.  If an R2L tab bar is
 14497      ever supported, display_tab_bar_line should also be augmented to
 14498      call unproduce_glyphs like display_line and display_string
 14499      do.  */
 14500   it.paragraph_embedding = L2R;
 14501 
 14502   /* Display as many lines as needed to display all tab-bar items.  */
 14503 
 14504   if (f->n_tab_bar_rows > 0)
 14505     {
 14506       int border, rows, height, extra;
 14507 
 14508       if (TYPE_RANGED_FIXNUMP (int, Vtab_bar_border))
 14509         border = XFIXNUM (Vtab_bar_border);
 14510       else if (EQ (Vtab_bar_border, Qinternal_border_width))
 14511         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 14512       else if (EQ (Vtab_bar_border, Qborder_width))
 14513         border = f->border_width;
 14514       else
 14515         border = 0;
 14516       if (border < 0)
 14517         border = 0;
 14518 
 14519       rows = f->n_tab_bar_rows;
 14520       height = max (1, (it.last_visible_y - border) / rows);
 14521       extra = it.last_visible_y - border - height * rows;
 14522 
 14523       while (it.current_y < it.last_visible_y)
 14524         {
 14525           int h = 0;
 14526           if (extra > 0 && rows-- > 0)
 14527             {
 14528               h = (extra + rows - 1) / rows;
 14529               extra -= h;
 14530             }
 14531           display_tab_bar_line (&it, height + h);
 14532         }
 14533     }
 14534   else
 14535     {
 14536       while (it.current_y < it.last_visible_y)
 14537         display_tab_bar_line (&it, 0);
 14538     }
 14539 
 14540   /* It doesn't make much sense to try scrolling in the tab-bar
 14541      window, so don't do it.  */
 14542   w->desired_matrix->no_scrolling_p = true;
 14543   w->must_be_updated_p = true;
 14544 
 14545   if (!NILP (Vauto_resize_tab_bars))
 14546     {
 14547       bool change_height_p = false;
 14548 
 14549       /* If we couldn't display everything, change the tab-bar's
 14550          height if there is room for more.  */
 14551       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 14552         change_height_p = true;
 14553 
 14554       /* We subtract 1 because display_tab_bar_line advances the
 14555          glyph_row pointer before returning to its caller.  We want to
 14556          examine the last glyph row produced by
 14557          display_tab_bar_line.  */
 14558       row = it.glyph_row - 1;
 14559 
 14560       /* If there are blank lines at the end, except for a partially
 14561          visible blank line at the end that is smaller than
 14562          FRAME_LINE_HEIGHT, change the tab-bar's height.  */
 14563       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14564           && row->height >= FRAME_LINE_HEIGHT (f))
 14565         change_height_p = true;
 14566 
 14567       /* If row displays tab-bar items, but is partially visible,
 14568          change the tab-bar's height.  */
 14569       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 14570           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 14571         change_height_p = true;
 14572 
 14573       /* Resize windows as needed by changing the `tab-bar-lines'
 14574          frame parameter.  */
 14575       if (change_height_p)
 14576         {
 14577           int nrows;
 14578           int new_height = tab_bar_height (f, &nrows, true);
 14579 
 14580           change_height_p = ((EQ (Vauto_resize_tab_bars, Qgrow_only)
 14581                               && !f->minimize_tab_bar_window_p)
 14582                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 14583                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 14584           f->minimize_tab_bar_window_p = false;
 14585 
 14586           if (change_height_p)
 14587             {
 14588               if (FRAME_TERMINAL (f)->change_tab_bar_height_hook)
 14589                 FRAME_TERMINAL (f)->change_tab_bar_height_hook (f, new_height);
 14590               frame_default_tab_bar_height = new_height;
 14591               clear_glyph_matrix (w->desired_matrix);
 14592               f->n_tab_bar_rows = nrows;
 14593               f->fonts_changed = true;
 14594 
 14595               return true;
 14596             }
 14597         }
 14598     }
 14599 
 14600   f->minimize_tab_bar_window_p = false;
 14601   return false;
 14602 }
 14603 
 14604 /* Get information about the tab-bar item which is displayed in GLYPH
 14605    on frame F.  Return in *PROP_IDX the index where tab-bar item
 14606    properties start in F->tab_bar_items.  Return in CLOSE_P an
 14607    indication whether the click was on the close-tab icon of the tab.
 14608    Value is false if GLYPH doesn't display a tab-bar item.  */
 14609 
 14610 static bool
 14611 tab_bar_item_info (struct frame *f, struct glyph *glyph,
 14612                    int *prop_idx, bool *close_p)
 14613 {
 14614   Lisp_Object prop;
 14615   ptrdiff_t charpos;
 14616 
 14617   /* This function can be called asynchronously, which means we must
 14618      exclude any possibility that Fget_text_property signals an
 14619      error.  */
 14620   charpos = min (SCHARS (f->current_tab_bar_string), glyph->charpos);
 14621   charpos = max (0, charpos);
 14622 
 14623   /* Get the text property `menu-item' at pos. The value of that
 14624      property is the start index of this item's properties in
 14625      F->tab_bar_items.  */
 14626   prop = Fget_text_property (make_fixnum (charpos),
 14627                              Qmenu_item, f->current_tab_bar_string);
 14628   if (! FIXNUMP (prop))
 14629     return false;
 14630   *prop_idx = XFIXNUM (prop);
 14631 
 14632   *close_p = !NILP (Fget_text_property (make_fixnum (charpos),
 14633                                         Qclose_tab,
 14634                                         f->current_tab_bar_string));
 14635 
 14636   return true;
 14637 }
 14638 
 14639 
 14640 /* Get information about the tab-bar item at position X/Y on frame F.
 14641    Return in *GLYPH a pointer to the glyph of the tab-bar item in
 14642    the current matrix of the tab-bar window of F, or NULL if not
 14643    on a tab-bar item.  Return in *PROP_IDX the index of the tab-bar
 14644    item in F->tab_bar_items.  Value is
 14645 
 14646    -1   if X/Y is not on a tab-bar item
 14647    0    if X/Y is on the same item that was highlighted before.
 14648    1    otherwise.  */
 14649 
 14650 static int
 14651 get_tab_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 14652                    int *hpos, int *vpos, int *prop_idx, bool *close_p)
 14653 {
 14654   struct window *w = XWINDOW (f->tab_bar_window);
 14655   int area;
 14656 
 14657   /* Find the glyph under X/Y.  */
 14658   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 14659   if (*glyph == NULL)
 14660     return -1;
 14661 
 14662   /* Get the start of this tab-bar item's properties in
 14663      f->tab_bar_items.  */
 14664   if (!tab_bar_item_info (f, *glyph, prop_idx, close_p))
 14665     return -1;
 14666 
 14667   return *prop_idx == f->last_tab_bar_item ? 0 : 1;
 14668 }
 14669 
 14670 
 14671 /* EXPORT:
 14672    Handle mouse button event on the tab-bar of frame F, at
 14673    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 14674    false for button release.  MODIFIERS is event modifiers for button
 14675    release.  */
 14676 
 14677 Lisp_Object
 14678 handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14679                       int modifiers)
 14680 {
 14681   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14682   struct window *w = XWINDOW (f->tab_bar_window);
 14683   int hpos, vpos, prop_idx;
 14684   bool close_p;
 14685   struct glyph *glyph;
 14686   Lisp_Object enabled_p;
 14687   int ts;
 14688 
 14689   frame_to_window_pixel_xy (w, &x, &y);
 14690   ts = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14691   if (ts == -1)
 14692     return Fcons (Qtab_bar, Qnil);
 14693 
 14694   /* If item is disabled, do nothing.  */
 14695   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14696   if (NILP (enabled_p))
 14697     return Qnil;
 14698 
 14699   if (down_p)
 14700     {
 14701       /* Show the clicked button in pressed state.  */
 14702       if (!NILP (Vmouse_highlight))
 14703         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 14704       f->last_tab_bar_item = prop_idx; /* record the pressed tab */
 14705     }
 14706   else
 14707     {
 14708       /* Show item in released state.  */
 14709       if (!NILP (Vmouse_highlight))
 14710         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 14711       f->last_tab_bar_item = -1;
 14712     }
 14713 
 14714   Lisp_Object caption =
 14715     Fcopy_sequence (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION));
 14716 
 14717   AUTO_LIST2 (props, Qmenu_item,
 14718               list3 (AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_KEY),
 14719                      AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_BINDING),
 14720                      close_p ? Qt : Qnil));
 14721 
 14722   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14723                         props, caption);
 14724 
 14725   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14726 }
 14727 
 14728 
 14729 /* Possibly highlight a tab-bar item on frame F when mouse moves to
 14730    tab-bar window-relative coordinates X/Y.  Called from
 14731    note_mouse_highlight.  */
 14732 
 14733 static void
 14734 note_tab_bar_highlight (struct frame *f, int x, int y)
 14735 {
 14736   Lisp_Object window = f->tab_bar_window;
 14737   struct window *w = XWINDOW (window);
 14738   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 14739   int hpos, vpos;
 14740   struct glyph *glyph;
 14741   struct glyph_row *row;
 14742   int i;
 14743   Lisp_Object enabled_p;
 14744   int prop_idx;
 14745   bool close_p;
 14746   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 14747   int rc;
 14748 
 14749   /* Function note_mouse_highlight is called with negative X/Y
 14750      values when mouse moves outside of the frame.  */
 14751   if (x <= 0 || y <= 0)
 14752     {
 14753       clear_mouse_face (hlinfo);
 14754       return;
 14755     }
 14756 
 14757   rc = get_tab_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx, &close_p);
 14758   if (rc < 0)
 14759     {
 14760       /* Not on tab-bar item.  */
 14761       clear_mouse_face (hlinfo);
 14762       return;
 14763     }
 14764   else if (rc == 0)
 14765     /* On same tab-bar item as before.  */
 14766     goto set_help_echo;
 14767 
 14768   clear_mouse_face (hlinfo);
 14769 
 14770   bool mouse_down_p = false;
 14771   /* Mouse is down, but on different tab-bar item?  Or alternatively,
 14772      the mouse might've been pressed somewhere we don't know about,
 14773      and then have moved onto the tab bar.  In this case,
 14774      last_tab_bar_item is -1, so we DTRT and behave like other
 14775      programs by displaying the item as sunken. */
 14776   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 14777   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 14778                   && f == dpyinfo->last_mouse_frame);
 14779 
 14780   if (mouse_down_p && f->last_tab_bar_item != prop_idx
 14781       && f->last_tab_bar_item != -1)
 14782     return;
 14783   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 14784 
 14785   /* If tab-bar item is not enabled, don't highlight it.  */
 14786   enabled_p = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_ENABLED_P);
 14787   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 14788     {
 14789       /* Compute the x-position of the glyph.  In front and past the
 14790          image is a space.  We include this in the highlighted area.  */
 14791       row = MATRIX_ROW (w->current_matrix, vpos);
 14792       for (i = x = 0; i < hpos; ++i)
 14793         x += row->glyphs[TEXT_AREA][i].pixel_width;
 14794 
 14795       /* Record this as the current active region.  */
 14796       hlinfo->mouse_face_beg_col = hpos;
 14797       hlinfo->mouse_face_beg_row = vpos;
 14798       hlinfo->mouse_face_beg_x = x;
 14799       hlinfo->mouse_face_past_end = false;
 14800 
 14801       hlinfo->mouse_face_end_col = hpos + 1;
 14802       hlinfo->mouse_face_end_row = vpos;
 14803       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 14804       hlinfo->mouse_face_window = window;
 14805       hlinfo->mouse_face_face_id = TAB_BAR_FACE_ID;
 14806 
 14807       /* Display it as active.  */
 14808       show_mouse_face (hlinfo, draw);
 14809     }
 14810 
 14811  set_help_echo:
 14812 
 14813   /* Set help_echo_string to a help string to display for this tab-bar item.
 14814      XTread_socket does the rest.  */
 14815   help_echo_object = help_echo_window = Qnil;
 14816   help_echo_pos = -1;
 14817   help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_HELP);
 14818   if (NILP (help_echo_string))
 14819     help_echo_string = AREF (f->tab_bar_items, prop_idx + TAB_BAR_ITEM_CAPTION);
 14820 }
 14821 
 14822 #endif /* HAVE_WINDOW_SYSTEM */
 14823 
 14824 /* Find the tab-bar item at X coordinate and return its information.  */
 14825 static Lisp_Object
 14826 tty_get_tab_bar_item (struct frame *f, int x, int *prop_idx, bool *close_p)
 14827 {
 14828   ptrdiff_t clen = 0;
 14829 
 14830   for (int i = 0; i < f->n_tab_bar_items; i++)
 14831     {
 14832       Lisp_Object caption = AREF (f->tab_bar_items, (i * TAB_BAR_ITEM_NSLOTS
 14833                                                      + TAB_BAR_ITEM_CAPTION));
 14834       if (NILP (caption))
 14835         return Qnil;
 14836       clen += SCHARS (caption);
 14837       if (x < clen)
 14838         {
 14839           *prop_idx = i;
 14840           *close_p = !NILP (Fget_text_property (make_fixnum (SCHARS (caption)
 14841                                                              - (clen - x)),
 14842                                                 Qclose_tab,
 14843                                                 caption));
 14844           return caption;
 14845         }
 14846     }
 14847   return Qnil;
 14848 }
 14849 
 14850 /* Handle a mouse click at X/Y on the tab bar of TTY frame F.  If the
 14851    click was on the tab bar and was handled, populate the EVENT
 14852    structure, store it in keyboard queue, and return true; otherwise
 14853    return false.  MODIFIERS are event modifiers for generating the tab
 14854    release event.  */
 14855 Lisp_Object
 14856 tty_handle_tab_bar_click (struct frame *f, int x, int y, bool down_p,
 14857                           struct input_event *event)
 14858 {
 14859   /* Did they click on the tab bar?  */
 14860   if (y < FRAME_MENU_BAR_LINES (f)
 14861       || y >= FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f))
 14862     return Qnil;
 14863 
 14864   /* Find the tab-bar item where the X,Y coordinates belong.  */
 14865   int prop_idx;
 14866   bool close_p;
 14867   Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &close_p);
 14868 
 14869   if (NILP (caption))
 14870     return Qnil;
 14871 
 14872   if (NILP (AREF (f->tab_bar_items,
 14873                   prop_idx * TAB_BAR_ITEM_NSLOTS + TAB_BAR_ITEM_ENABLED_P)))
 14874     return Qnil;
 14875 
 14876   if (down_p)
 14877     f->last_tab_bar_item = prop_idx;
 14878   else
 14879     f->last_tab_bar_item = -1;
 14880 
 14881   caption = Fcopy_sequence (caption);
 14882 
 14883   AUTO_LIST2 (props, Qmenu_item,
 14884               list3 (AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14885                            + TAB_BAR_ITEM_KEY),
 14886                      AREF (f->tab_bar_items, prop_idx * TAB_BAR_ITEM_NSLOTS
 14887                            + TAB_BAR_ITEM_BINDING),
 14888                      close_p ? Qt : Qnil));
 14889 
 14890   Fadd_text_properties (make_fixnum (0), make_fixnum (SCHARS (caption)),
 14891                         props, caption);
 14892 
 14893   return Fcons (Qtab_bar, Fcons (caption, make_fixnum (0)));
 14894 }
 14895 
 14896 
 14897 
 14898 /***********************************************************************
 14899                                Tool-bars
 14900  ***********************************************************************/
 14901 
 14902 #ifdef HAVE_WINDOW_SYSTEM
 14903 
 14904 /* Update the tool-bar item list for frame F.  This has to be done
 14905    before we start to fill in any display lines.  Called from
 14906    prepare_menu_bars.  If SAVE_MATCH_DATA, we must save
 14907    and restore it here.  */
 14908 
 14909 static void
 14910 update_tool_bar (struct frame *f, bool save_match_data)
 14911 {
 14912 #ifdef HAVE_EXT_TOOL_BAR
 14913   bool do_update = FRAME_EXTERNAL_TOOL_BAR (f);
 14914 #else
 14915   bool do_update = (WINDOWP (f->tool_bar_window)
 14916                     && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0);
 14917 #endif
 14918 
 14919   if (do_update)
 14920     {
 14921       Lisp_Object window;
 14922       struct window *w;
 14923 
 14924       window = FRAME_SELECTED_WINDOW (f);
 14925       w = XWINDOW (window);
 14926 
 14927       /* If the user has switched buffers or windows, we need to
 14928          recompute to reflect the new bindings.  But we'll
 14929          recompute when update_mode_lines is set too; that means
 14930          that people can use force-mode-line-update to request
 14931          that the menu bar be recomputed.  The adverse effect on
 14932          the rest of the redisplay algorithm is about the same as
 14933          windows_or_buffers_changed anyway.  */
 14934       if (windows_or_buffers_changed
 14935           || w->update_mode_line
 14936           || update_mode_lines
 14937           || window_buffer_changed (w))
 14938         {
 14939           struct buffer *prev = current_buffer;
 14940           specpdl_ref count = SPECPDL_INDEX ();
 14941           Lisp_Object frame, new_tool_bar;
 14942           int new_n_tool_bar;
 14943 
 14944           /* Set current_buffer to the buffer of the selected
 14945              window of the frame, so that we get the right local
 14946              keymaps.  */
 14947           set_buffer_internal_1 (XBUFFER (w->contents));
 14948 
 14949           /* Save match data, if we must.  */
 14950           if (save_match_data)
 14951             record_unwind_save_match_data ();
 14952 
 14953           /* Make sure that we don't accidentally use bogus keymaps.  */
 14954           if (NILP (Voverriding_local_map_menu_flag))
 14955             {
 14956               specbind (Qoverriding_terminal_local_map, Qnil);
 14957               specbind (Qoverriding_local_map, Qnil);
 14958             }
 14959 
 14960           /* We must temporarily set the selected frame to this frame
 14961              before calling tool_bar_items, because the calculation of
 14962              the tool-bar keymap uses the selected frame (see
 14963              `tool-bar-make-keymap' in tool-bar.el).  */
 14964           eassert (EQ (selected_window,
 14965                        /* Since we only explicitly preserve selected_frame,
 14966                           check that selected_window would be redundant.  */
 14967                        XFRAME (selected_frame)->selected_window));
 14968           record_unwind_protect (restore_selected_window, selected_window);
 14969           XSETFRAME (frame, f);
 14970           selected_frame = frame;
 14971           selected_window = FRAME_SELECTED_WINDOW (f);
 14972 
 14973           /* Build desired tool-bar items from keymaps.  */
 14974           new_tool_bar
 14975             = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
 14976                               &new_n_tool_bar);
 14977 
 14978           /* Redisplay the tool-bar if we changed it.  */
 14979           if (new_n_tool_bar != f->n_tool_bar_items
 14980               || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
 14981             {
 14982               /* Redisplay that happens asynchronously due to an expose event
 14983                  may access f->tool_bar_items.  Make sure we update both
 14984                  variables within BLOCK_INPUT so no such event interrupts.  */
 14985               block_input ();
 14986               fset_tool_bar_items (f, new_tool_bar);
 14987               f->n_tool_bar_items = new_n_tool_bar;
 14988               w->update_mode_line = true;
 14989               unblock_input ();
 14990             }
 14991 
 14992           unbind_to (count, Qnil);
 14993           set_buffer_internal_1 (prev);
 14994         }
 14995     }
 14996 }
 14997 
 14998 #ifndef HAVE_EXT_TOOL_BAR
 14999 
 15000 /* Set F->desired_tool_bar_string to a Lisp string representing frame
 15001    F's desired tool-bar contents.  F->tool_bar_items must have
 15002    been set up previously by calling prepare_menu_bars.  */
 15003 
 15004 static void
 15005 build_desired_tool_bar_string (struct frame *f)
 15006 {
 15007   int i, size, size_needed;
 15008   Lisp_Object image, plist;
 15009 
 15010   image = plist = Qnil;
 15011 
 15012   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
 15013      Otherwise, make a new string.  */
 15014 
 15015   /* The size of the string we might be able to reuse.  */
 15016   size = (STRINGP (f->desired_tool_bar_string)
 15017           ? SCHARS (f->desired_tool_bar_string)
 15018           : 0);
 15019 
 15020   /* We need one space in the string for each image.  */
 15021   size_needed = f->n_tool_bar_items;
 15022 
 15023   /* Reuse f->desired_tool_bar_string, if possible.  */
 15024   if (size < size_needed || NILP (f->desired_tool_bar_string))
 15025     fset_desired_tool_bar_string
 15026       (f, Fmake_string (make_fixnum (size_needed), make_fixnum (' '), Qnil));
 15027   else
 15028     {
 15029       AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
 15030       Fremove_text_properties (make_fixnum (0), make_fixnum (size),
 15031                                props, f->desired_tool_bar_string);
 15032     }
 15033 
 15034   /* Put a `display' property on the string for the images to display,
 15035      put a `menu_item' property on tool-bar items with a value that
 15036      is the index of the item in F's tool-bar item vector.  */
 15037   for (i = 0; i < f->n_tool_bar_items; ++i)
 15038     {
 15039 #define PROP(IDX) \
 15040   AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
 15041 
 15042       bool enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
 15043       bool selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
 15044       int hmargin, vmargin, relief, idx, end;
 15045 
 15046       /* If image is a vector, choose the image according to the
 15047          button state.  */
 15048       image = PROP (TOOL_BAR_ITEM_IMAGES);
 15049       if (VECTORP (image))
 15050         {
 15051           if (enabled_p)
 15052             idx = (selected_p
 15053                    ? TOOL_BAR_IMAGE_ENABLED_SELECTED
 15054                    : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
 15055           else
 15056             idx = (selected_p
 15057                    ? TOOL_BAR_IMAGE_DISABLED_SELECTED
 15058                    : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
 15059 
 15060           eassert (ASIZE (image) >= idx);
 15061           image = AREF (image, idx);
 15062         }
 15063       else
 15064         idx = -1;
 15065 
 15066       /* Ignore invalid image specifications.  */
 15067       if (!valid_image_p (image))
 15068         continue;
 15069 
 15070       /* Display the tool-bar button pressed, or depressed.  */
 15071       plist = Fcopy_sequence (XCDR (image));
 15072 
 15073       /* Compute margin and relief to draw.  */
 15074       relief = (tool_bar_button_relief >= 0
 15075                 ? min (tool_bar_button_relief,
 15076                        min (INT_MAX, MOST_POSITIVE_FIXNUM))
 15077                 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
 15078       hmargin = vmargin = relief;
 15079 
 15080       if (RANGED_FIXNUMP (1, Vtool_bar_button_margin,
 15081                            INT_MAX - max (hmargin, vmargin)))
 15082         {
 15083           hmargin += XFIXNAT (Vtool_bar_button_margin);
 15084           vmargin += XFIXNAT (Vtool_bar_button_margin);
 15085         }
 15086       else if (CONSP (Vtool_bar_button_margin))
 15087         {
 15088           if (RANGED_FIXNUMP (1, XCAR (Vtool_bar_button_margin),
 15089                                INT_MAX - hmargin))
 15090             hmargin += XFIXNAT (XCAR (Vtool_bar_button_margin));
 15091 
 15092           if (RANGED_FIXNUMP (1, XCDR (Vtool_bar_button_margin),
 15093                                INT_MAX - vmargin))
 15094             vmargin += XFIXNAT (XCDR (Vtool_bar_button_margin));
 15095         }
 15096 
 15097       if (auto_raise_tool_bar_buttons_p)
 15098         {
 15099           /* Add a `:relief' property to the image spec if the item is
 15100              selected.  */
 15101           if (selected_p)
 15102             {
 15103               plist = plist_put (plist, QCrelief, make_fixnum (-relief));
 15104               hmargin -= relief;
 15105               vmargin -= relief;
 15106             }
 15107         }
 15108       else
 15109         {
 15110           /* If image is selected, display it pressed, i.e. with a
 15111              negative relief.  If it's not selected, display it with a
 15112              raised relief.  */
 15113           plist = plist_put (plist, QCrelief,
 15114                              (selected_p
 15115                               ? make_fixnum (-relief)
 15116                               : make_fixnum (relief)));
 15117           hmargin -= relief;
 15118           vmargin -= relief;
 15119         }
 15120 
 15121       /* Put a margin around the image.  */
 15122       if (hmargin || vmargin)
 15123         {
 15124           if (hmargin == vmargin)
 15125             plist = plist_put (plist, QCmargin, make_fixnum (hmargin));
 15126           else
 15127             plist = plist_put (plist, QCmargin,
 15128                                Fcons (make_fixnum (hmargin),
 15129                                       make_fixnum (vmargin)));
 15130         }
 15131 
 15132       /* If button is not enabled, and we don't have special images
 15133          for the disabled state, make the image appear disabled by
 15134          applying an appropriate algorithm to it.  */
 15135       if (!enabled_p && idx < 0)
 15136         plist = plist_put (plist, QCconversion, Qdisabled);
 15137 
 15138       /* Put a `display' text property on the string for the image to
 15139          display.  Put a `menu-item' property on the string that gives
 15140          the start of this item's properties in the tool-bar items
 15141          vector.  */
 15142       image = Fcons (Qimage, plist);
 15143       AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
 15144                   make_fixnum (i * TOOL_BAR_ITEM_NSLOTS));
 15145 
 15146       /* Let the last image hide all remaining spaces in the tool bar
 15147          string.  The string can be longer than needed when we reuse a
 15148          previous string.  */
 15149       if (i + 1 == f->n_tool_bar_items)
 15150         end = SCHARS (f->desired_tool_bar_string);
 15151       else
 15152         end = i + 1;
 15153       Fadd_text_properties (make_fixnum (i), make_fixnum (end),
 15154                             props, f->desired_tool_bar_string);
 15155 #undef PROP
 15156     }
 15157 }
 15158 
 15159 
 15160 /* Display one line of the tool-bar of frame IT->f.
 15161 
 15162    HEIGHT specifies the desired height of the tool-bar line.
 15163    If the actual height of the glyph row is less than HEIGHT, the
 15164    row's height is increased to HEIGHT, and the icons are centered
 15165    vertically in the new height.
 15166 
 15167    If HEIGHT is -1, we are counting needed tool-bar lines, so don't
 15168    count a final empty row in case the tool-bar width exactly matches
 15169    the window width.
 15170 */
 15171 
 15172 static void
 15173 display_tool_bar_line (struct it *it, int height)
 15174 {
 15175   struct glyph_row *row = it->glyph_row;
 15176   int max_x = it->last_visible_x;
 15177   struct glyph *last;
 15178 
 15179   /* Don't extend on a previously drawn tool bar items (Bug#16058).  */
 15180   clear_glyph_row (row);
 15181   row->enabled_p = true;
 15182   row->y = it->current_y;
 15183 
 15184   /* Note that this isn't made use of if the face hasn't a box,
 15185      so there's no need to check the face here.  */
 15186   it->start_of_box_run_p = true;
 15187 
 15188   while (it->current_x < max_x)
 15189     {
 15190       int x, n_glyphs_before, i, nglyphs;
 15191       struct it it_before;
 15192 
 15193       /* Get the next display element.  */
 15194       if (!get_next_display_element (it))
 15195         {
 15196           /* Don't count empty row if we are counting needed tool-bar lines.  */
 15197           if (height < 0 && !it->hpos)
 15198             return;
 15199           break;
 15200         }
 15201 
 15202       /* Produce glyphs.  */
 15203       n_glyphs_before = row->used[TEXT_AREA];
 15204       it_before = *it;
 15205 
 15206       PRODUCE_GLYPHS (it);
 15207 
 15208       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 15209       i = 0;
 15210       x = it_before.current_x;
 15211       while (i < nglyphs)
 15212         {
 15213           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 15214 
 15215           if (x + glyph->pixel_width > max_x)
 15216             {
 15217               /* Glyph doesn't fit on line.  Backtrack.  */
 15218               row->used[TEXT_AREA] = n_glyphs_before;
 15219               *it = it_before;
 15220               /* If this is the only glyph on this line, it will never fit on the
 15221                  tool-bar, so skip it.  But ensure there is at least one glyph,
 15222                  so we don't accidentally disable the tool-bar.  */
 15223               if (n_glyphs_before == 0
 15224                   && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
 15225                 break;
 15226               goto out;
 15227             }
 15228 
 15229           ++it->hpos;
 15230           x += glyph->pixel_width;
 15231           ++i;
 15232         }
 15233 
 15234       /* Stop at line end.  */
 15235       if (ITERATOR_AT_END_OF_LINE_P (it))
 15236         break;
 15237 
 15238       set_iterator_to_next (it, true);
 15239     }
 15240 
 15241  out:;
 15242 
 15243   row->displays_text_p = row->used[TEXT_AREA] != 0;
 15244 
 15245   /* Use default face for the border below the tool bar.
 15246 
 15247      FIXME: When auto-resize-tool-bars is grow-only, there is
 15248      no additional border below the possibly empty tool-bar lines.
 15249      So to make the extra empty lines look "normal", we have to
 15250      use the tool-bar face for the border too.  */
 15251   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15252       && !EQ (Vauto_resize_tool_bars, Qgrow_only))
 15253     it->face_id = DEFAULT_FACE_ID;
 15254 
 15255   extend_face_to_end_of_line (it);
 15256   last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 15257   last->right_box_line_p = true;
 15258   if (last == row->glyphs[TEXT_AREA])
 15259     last->left_box_line_p = true;
 15260 
 15261   /* Make line the desired height and center it vertically.  */
 15262   if ((height -= it->max_ascent + it->max_descent) > 0)
 15263     {
 15264       /* Don't add more than one line height.  */
 15265       height %= FRAME_LINE_HEIGHT (it->f);
 15266       it->max_ascent += height / 2;
 15267       it->max_descent += (height + 1) / 2;
 15268     }
 15269 
 15270   compute_line_metrics (it);
 15271 
 15272   /* If line is empty, make it occupy the rest of the tool-bar.  */
 15273   if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
 15274     {
 15275       row->height = row->phys_height = it->last_visible_y - row->y;
 15276       row->visible_height = row->height;
 15277       row->ascent = row->phys_ascent = 0;
 15278       row->extra_line_spacing = 0;
 15279     }
 15280 
 15281   row->full_width_p = true;
 15282   row->continued_p = false;
 15283   row->truncated_on_left_p = false;
 15284   row->truncated_on_right_p = false;
 15285 
 15286   it->current_x = it->hpos = 0;
 15287   it->current_y += row->height;
 15288   ++it->vpos;
 15289   ++it->glyph_row;
 15290 }
 15291 
 15292 
 15293 /* Value is the number of pixels needed to make all tool-bar items of
 15294    frame F visible.  The actual number of glyph rows needed is
 15295    returned in *N_ROWS if non-NULL.  */
 15296 static int
 15297 tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
 15298 {
 15299   struct window *w = XWINDOW (f->tool_bar_window);
 15300   struct it it;
 15301   /* tool_bar_height is called from redisplay_tool_bar after building
 15302      the desired matrix, so use (unused) mode-line row as temporary row to
 15303      avoid destroying the first tool-bar row.  */
 15304   struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
 15305 
 15306   /* Initialize an iterator for iteration over
 15307      F->desired_tool_bar_string in the tool-bar window of frame F.  */
 15308   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
 15309   temp_row->reversed_p = false;
 15310   it.first_visible_x = 0;
 15311   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15312   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15313                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15314   it.paragraph_embedding = L2R;
 15315 
 15316   while (!ITERATOR_AT_END_P (&it))
 15317     {
 15318       clear_glyph_row (temp_row);
 15319       it.glyph_row = temp_row;
 15320       display_tool_bar_line (&it, -1);
 15321     }
 15322   clear_glyph_row (temp_row);
 15323 
 15324   /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar.  */
 15325   if (n_rows)
 15326     *n_rows = it.vpos > 0 ? it.vpos : -1;
 15327 
 15328   if (pixelwise)
 15329     return it.current_y;
 15330   else
 15331     return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
 15332 }
 15333 
 15334 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15335 
 15336 DEFUN ("tool-bar-height", Ftool_bar_height, Stool_bar_height,
 15337        0, 2, 0,
 15338        doc: /* Return the number of lines occupied by the tool bar of FRAME.
 15339 If FRAME is nil or omitted, use the selected frame.  Optional argument
 15340 PIXELWISE non-nil means return the height of the tool bar in pixels.  */)
 15341   (Lisp_Object frame, Lisp_Object pixelwise)
 15342 {
 15343   int height = 0;
 15344 
 15345 #ifndef HAVE_EXT_TOOL_BAR
 15346   struct frame *f = decode_any_frame (frame);
 15347 
 15348   if (WINDOWP (f->tool_bar_window)
 15349       && WINDOW_PIXEL_HEIGHT (XWINDOW (f->tool_bar_window)) > 0)
 15350     {
 15351       update_tool_bar (f, true);
 15352       if (f->n_tool_bar_items)
 15353         {
 15354           build_desired_tool_bar_string (f);
 15355           height = tool_bar_height (f, NULL, !NILP (pixelwise));
 15356         }
 15357     }
 15358 #endif
 15359 
 15360   return make_fixnum (height);
 15361 }
 15362 
 15363 #ifndef HAVE_EXT_TOOL_BAR
 15364 
 15365 /* Display the internal tool-bar of frame F.  Value is true if
 15366    tool-bar's height should be changed.  */
 15367 static bool
 15368 redisplay_tool_bar (struct frame *f)
 15369 {
 15370   struct window *w;
 15371   struct it it;
 15372   struct glyph_row *row;
 15373 
 15374   f->tool_bar_redisplayed = true;
 15375 
 15376   /* If frame hasn't a tool-bar window or if it is zero-height, don't
 15377      do anything.  This means you must start with tool-bar-lines
 15378      non-zero to get the auto-sizing effect.  Or in other words, you
 15379      can turn off tool-bars by specifying tool-bar-lines zero.  */
 15380   if (!WINDOWP (f->tool_bar_window)
 15381       || (w = XWINDOW (f->tool_bar_window),
 15382           WINDOW_TOTAL_LINES (w) == 0))
 15383     {
 15384       /* Even if we do not display a tool bar initially, still pretend
 15385          that we have resized it already.  This avoids that a later
 15386          activation of the tool bar resizes the frame, despite of the
 15387          fact that a setting of 'frame-inhibit-implied-resize' should
 15388          inhibit it (Bug#52986).  */
 15389       f->tool_bar_resized = true;
 15390 
 15391       return false;
 15392     }
 15393 
 15394   /* Set up an iterator for the tool-bar window.  */
 15395   init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
 15396   it.first_visible_x = 0;
 15397   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
 15398   row = it.glyph_row;
 15399   row->reversed_p = false;
 15400 
 15401   /* Build a string that represents the contents of the tool-bar.  */
 15402   build_desired_tool_bar_string (f);
 15403   reseat_to_string (&it, NULL, f->desired_tool_bar_string,
 15404                     0, 0, 0, STRING_MULTIBYTE (f->desired_tool_bar_string));
 15405   /* FIXME: This should be controlled by a user option.  But it
 15406      doesn't make sense to have an R2L tool bar if the menu bar cannot
 15407      be drawn also R2L, and making the menu bar R2L is tricky due to
 15408      toolkit-specific code that implements it.  If an R2L tool bar is
 15409      ever supported, display_tool_bar_line should also be augmented to
 15410      call unproduce_glyphs like display_line and display_string
 15411      do.  */
 15412   it.paragraph_embedding = L2R;
 15413 
 15414   if (f->n_tool_bar_rows == 0)
 15415     {
 15416       int new_height = tool_bar_height (f, &f->n_tool_bar_rows, true);
 15417 
 15418       if (new_height != WINDOW_PIXEL_HEIGHT (w))
 15419         {
 15420           if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15421             FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15422           frame_default_tool_bar_height = new_height;
 15423           /* Always do that now.  */
 15424           clear_glyph_matrix (w->desired_matrix);
 15425           f->fonts_changed = true;
 15426           return true;
 15427         }
 15428     }
 15429 
 15430   /* Display as many lines as needed to display all tool-bar items.  */
 15431 
 15432   if (f->n_tool_bar_rows > 0)
 15433     {
 15434       int border, rows, height, extra;
 15435 
 15436       if (TYPE_RANGED_FIXNUMP (int, Vtool_bar_border))
 15437         border = XFIXNUM (Vtool_bar_border);
 15438       else if (EQ (Vtool_bar_border, Qinternal_border_width))
 15439         border = FRAME_INTERNAL_BORDER_WIDTH (f);
 15440       else if (EQ (Vtool_bar_border, Qborder_width))
 15441         border = f->border_width;
 15442       else
 15443         border = 0;
 15444       if (border < 0)
 15445         border = 0;
 15446 
 15447       rows = f->n_tool_bar_rows;
 15448       height = max (1, (it.last_visible_y - border) / rows);
 15449       extra = it.last_visible_y - border - height * rows;
 15450 
 15451       while (it.current_y < it.last_visible_y)
 15452         {
 15453           int h = 0;
 15454           if (extra > 0 && rows-- > 0)
 15455             {
 15456               h = (extra + rows - 1) / rows;
 15457               extra -= h;
 15458             }
 15459           display_tool_bar_line (&it, height + h);
 15460         }
 15461     }
 15462   else
 15463     {
 15464       while (it.current_y < it.last_visible_y)
 15465         display_tool_bar_line (&it, 0);
 15466     }
 15467 
 15468   /* It doesn't make much sense to try scrolling in the tool-bar
 15469      window, so don't do it.  */
 15470   w->desired_matrix->no_scrolling_p = true;
 15471   w->must_be_updated_p = true;
 15472 
 15473   if (!NILP (Vauto_resize_tool_bars))
 15474     {
 15475       bool change_height_p = false;
 15476 
 15477       /* If we couldn't display everything, change the tool-bar's
 15478          height if there is room for more.  */
 15479       if (IT_STRING_CHARPOS (it) < it.end_charpos)
 15480         change_height_p = true;
 15481 
 15482       /* We subtract 1 because display_tool_bar_line advances the
 15483          glyph_row pointer before returning to its caller.  We want to
 15484          examine the last glyph row produced by
 15485          display_tool_bar_line.  */
 15486       row = it.glyph_row - 1;
 15487 
 15488       /* If there are blank lines at the end, except for a partially
 15489          visible blank line at the end that is smaller than
 15490          FRAME_LINE_HEIGHT, change the tool-bar's height.  */
 15491       if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15492           && row->height >= FRAME_LINE_HEIGHT (f))
 15493         change_height_p = true;
 15494 
 15495       /* If row displays tool-bar items, but is partially visible,
 15496          change the tool-bar's height.  */
 15497       if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 15498           && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y)
 15499         change_height_p = true;
 15500 
 15501       /* Resize windows as needed by changing the `tool-bar-lines'
 15502          frame parameter.  */
 15503       if (change_height_p)
 15504         {
 15505           int nrows;
 15506           int new_height = tool_bar_height (f, &nrows, true);
 15507 
 15508           change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
 15509                               && !f->minimize_tool_bar_window_p)
 15510                              ? (new_height > WINDOW_PIXEL_HEIGHT (w))
 15511                              : (new_height != WINDOW_PIXEL_HEIGHT (w)));
 15512           f->minimize_tool_bar_window_p = false;
 15513 
 15514           if (change_height_p)
 15515             {
 15516               if (FRAME_TERMINAL (f)->change_tool_bar_height_hook)
 15517                 FRAME_TERMINAL (f)->change_tool_bar_height_hook (f, new_height);
 15518               frame_default_tool_bar_height = new_height;
 15519               clear_glyph_matrix (w->desired_matrix);
 15520               f->n_tool_bar_rows = nrows;
 15521               f->fonts_changed = true;
 15522 
 15523               return true;
 15524             }
 15525         }
 15526     }
 15527 
 15528   f->minimize_tool_bar_window_p = false;
 15529 
 15530   return false;
 15531 }
 15532 
 15533 /* Get information about the tool-bar item which is displayed in GLYPH
 15534    on frame F.  Return in *PROP_IDX the index where tool-bar item
 15535    properties start in F->tool_bar_items.  Value is false if
 15536    GLYPH doesn't display a tool-bar item.  */
 15537 
 15538 static bool
 15539 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
 15540 {
 15541   Lisp_Object prop;
 15542   ptrdiff_t charpos;
 15543 
 15544   /* This function can be called asynchronously, which means we must
 15545      exclude any possibility that Fget_text_property signals an
 15546      error.  */
 15547   charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
 15548   charpos = max (0, charpos);
 15549 
 15550   /* Get the text property `menu-item' at pos. The value of that
 15551      property is the start index of this item's properties in
 15552      F->tool_bar_items.  */
 15553   prop = Fget_text_property (make_fixnum (charpos),
 15554                              Qmenu_item, f->current_tool_bar_string);
 15555   if (! FIXNUMP (prop))
 15556     return false;
 15557   *prop_idx = XFIXNUM (prop);
 15558   return true;
 15559 }
 15560 
 15561 
 15562 /* Get information about the tool-bar item at position X/Y on frame F.
 15563    Return in *GLYPH a pointer to the glyph of the tool-bar item in
 15564    the current matrix of the tool-bar window of F, or NULL if not
 15565    on a tool-bar item.  Return in *PROP_IDX the index of the tool-bar
 15566    item in F->tool_bar_items.  Value is
 15567 
 15568    -1   if X/Y is not on a tool-bar item
 15569    0    if X/Y is on the same item that was highlighted before.
 15570    1    otherwise.  */
 15571 
 15572 static int
 15573 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
 15574                    int *hpos, int *vpos, int *prop_idx)
 15575 {
 15576   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15577   struct window *w = XWINDOW (f->tool_bar_window);
 15578   int area;
 15579 
 15580   /* Find the glyph under X/Y.  */
 15581   *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
 15582   if (*glyph == NULL)
 15583     return -1;
 15584 
 15585   /* Get the start of this tool-bar item's properties in
 15586      f->tool_bar_items.  */
 15587   if (!tool_bar_item_info (f, *glyph, prop_idx))
 15588     return -1;
 15589 
 15590   /* Is mouse on the highlighted item?  */
 15591   if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
 15592       && *vpos >= hlinfo->mouse_face_beg_row
 15593       && *vpos <= hlinfo->mouse_face_end_row
 15594       && (*vpos > hlinfo->mouse_face_beg_row
 15595           || *hpos >= hlinfo->mouse_face_beg_col)
 15596       && (*vpos < hlinfo->mouse_face_end_row
 15597           || *hpos < hlinfo->mouse_face_end_col
 15598           || hlinfo->mouse_face_past_end))
 15599     return 0;
 15600 
 15601   return 1;
 15602 }
 15603 
 15604 
 15605 /* EXPORT:
 15606    Handle mouse button event on the tool-bar of frame F, at
 15607    frame-relative coordinates X/Y.  DOWN_P is true for a button press,
 15608    false for button release.  MODIFIERS is event modifiers for button
 15609    release.  DEVICE is the device the click came from, or Qt.  */
 15610 
 15611 void
 15612 handle_tool_bar_click_with_device (struct frame *f, int x, int y, bool down_p,
 15613                                    int modifiers, Lisp_Object device)
 15614 {
 15615   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15616   struct window *w = XWINDOW (f->tool_bar_window);
 15617   int hpos, vpos, prop_idx;
 15618   struct glyph *glyph;
 15619   Lisp_Object enabled_p;
 15620   int ts;
 15621 
 15622   /* If not on the highlighted tool-bar item, and mouse-highlight is
 15623      non-nil, return.  This is so we generate the tool-bar button
 15624      click only when the mouse button is released on the same item as
 15625      where it was pressed.  However, when mouse-highlight is disabled,
 15626      generate the click when the button is released regardless of the
 15627      highlight, since tool-bar items are not highlighted in that
 15628      case.  */
 15629   frame_to_window_pixel_xy (w, &x, &y);
 15630   ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15631   if (ts == -1
 15632       || (ts != 0 && !NILP (Vmouse_highlight)))
 15633     return;
 15634 
 15635   /* When mouse-highlight is off, generate the click for the item
 15636      where the button was pressed, disregarding where it was
 15637      released.  */
 15638   if (NILP (Vmouse_highlight) && !down_p)
 15639     prop_idx = f->last_tool_bar_item;
 15640 
 15641   /* If item is disabled, do nothing.  */
 15642   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15643   if (NILP (enabled_p))
 15644     return;
 15645 
 15646   if (down_p)
 15647     {
 15648       /* Show item in pressed state.  */
 15649       if (!NILP (Vmouse_highlight))
 15650         show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
 15651       f->last_tool_bar_item = prop_idx;
 15652     }
 15653   else
 15654     {
 15655       Lisp_Object key, frame;
 15656       struct input_event event;
 15657       EVENT_INIT (event);
 15658 
 15659       /* Show item in released state.  */
 15660       if (!NILP (Vmouse_highlight))
 15661         show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
 15662 
 15663       key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
 15664 
 15665       XSETFRAME (frame, f);
 15666       event.kind = TOOL_BAR_EVENT;
 15667       event.frame_or_window = frame;
 15668       event.arg = key;
 15669       event.modifiers = modifiers;
 15670       event.device = device;
 15671       kbd_buffer_store_event (&event);
 15672       f->last_tool_bar_item = -1;
 15673     }
 15674 }
 15675 
 15676 void
 15677 handle_tool_bar_click (struct frame *f, int x, int y, bool down_p,
 15678                        int modifiers)
 15679 {
 15680   handle_tool_bar_click_with_device (f, x, y, down_p, modifiers, Qt);
 15681 }
 15682 
 15683 /* Possibly highlight a tool-bar item on frame F when mouse moves to
 15684    tool-bar window-relative coordinates X/Y.  Called from
 15685    note_mouse_highlight.  */
 15686 
 15687 static void
 15688 note_tool_bar_highlight (struct frame *f, int x, int y)
 15689 {
 15690   Lisp_Object window = f->tool_bar_window;
 15691   struct window *w = XWINDOW (window);
 15692   Display_Info *dpyinfo = FRAME_DISPLAY_INFO (f);
 15693   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 15694   int hpos, vpos;
 15695   struct glyph *glyph;
 15696   struct glyph_row *row;
 15697   int i;
 15698   Lisp_Object enabled_p;
 15699   int prop_idx;
 15700   enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
 15701   bool mouse_down_p;
 15702   int rc;
 15703 
 15704   /* Function note_mouse_highlight is called with negative X/Y
 15705      values when mouse moves outside of the frame.  */
 15706   if (x <= 0 || y <= 0)
 15707     {
 15708       clear_mouse_face (hlinfo);
 15709       return;
 15710     }
 15711 
 15712   rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
 15713   if (rc < 0)
 15714     {
 15715       /* Not on tool-bar item.  */
 15716       clear_mouse_face (hlinfo);
 15717       return;
 15718     }
 15719   else if (rc == 0)
 15720     /* On same tool-bar item as before.  */
 15721     goto set_help_echo;
 15722 
 15723   clear_mouse_face (hlinfo);
 15724 
 15725   /* Mouse is down, but on different tool-bar item?  */
 15726   mouse_down_p = (gui_mouse_grabbed (dpyinfo)
 15727                   && f == dpyinfo->last_mouse_frame);
 15728 
 15729   if (mouse_down_p && f->last_tool_bar_item != prop_idx)
 15730     return;
 15731 
 15732   draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
 15733 
 15734   /* If tool-bar item is not enabled, don't highlight it.  */
 15735   enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
 15736   if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
 15737     {
 15738       /* Compute the x-position of the glyph.  In front and past the
 15739          image is a space.  We include this in the highlighted area.  */
 15740       row = MATRIX_ROW (w->current_matrix, vpos);
 15741       for (i = x = 0; i < hpos; ++i)
 15742         x += row->glyphs[TEXT_AREA][i].pixel_width;
 15743 
 15744       /* Record this as the current active region.  */
 15745       hlinfo->mouse_face_beg_col = hpos;
 15746       hlinfo->mouse_face_beg_row = vpos;
 15747       hlinfo->mouse_face_beg_x = x;
 15748       hlinfo->mouse_face_past_end = false;
 15749 
 15750       hlinfo->mouse_face_end_col = hpos + 1;
 15751       hlinfo->mouse_face_end_row = vpos;
 15752       hlinfo->mouse_face_end_x = x + glyph->pixel_width;
 15753       hlinfo->mouse_face_window = window;
 15754       hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
 15755 
 15756       /* Display it as active.  */
 15757       show_mouse_face (hlinfo, draw);
 15758     }
 15759 
 15760  set_help_echo:
 15761 
 15762   /* Set help_echo_string to a help string to display for this tool-bar item.
 15763      XTread_socket does the rest.  */
 15764   help_echo_object = help_echo_window = Qnil;
 15765   help_echo_pos = -1;
 15766   help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
 15767   if (NILP (help_echo_string))
 15768     help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
 15769 }
 15770 
 15771 #endif /* ! (HAVE_EXT_TOOL_BAR) */
 15772 
 15773 #endif /* HAVE_WINDOW_SYSTEM */
 15774 
 15775 
 15776 
 15777 /************************************************************************
 15778                          Horizontal scrolling
 15779  ************************************************************************/
 15780 
 15781 /* For all leaf windows in the window tree rooted at WINDOW, set their
 15782    hscroll value so that PT is (i) visible in the window, and (ii) so
 15783    that it is not within a certain margin at the window's left and
 15784    right border.  Value is true if any window's hscroll has been
 15785    changed.  */
 15786 
 15787 static bool
 15788 hscroll_window_tree (Lisp_Object window)
 15789 {
 15790   bool hscrolled_p = false;
 15791   bool hscroll_relative_p = FLOATP (Vhscroll_step);
 15792   int hscroll_step_abs = 0;
 15793   double hscroll_step_rel = 0;
 15794 
 15795   if (hscroll_relative_p)
 15796     {
 15797       hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
 15798       if (hscroll_step_rel < 0)
 15799         {
 15800           hscroll_relative_p = false;
 15801           hscroll_step_abs = 0;
 15802         }
 15803     }
 15804   else if (TYPE_RANGED_FIXNUMP (int, Vhscroll_step))
 15805     {
 15806       hscroll_step_abs = XFIXNUM (Vhscroll_step);
 15807       if (hscroll_step_abs < 0)
 15808         hscroll_step_abs = 0;
 15809     }
 15810   else
 15811     hscroll_step_abs = 0;
 15812 
 15813   while (WINDOWP (window))
 15814     {
 15815       struct window *w = XWINDOW (window);
 15816 
 15817       if (WINDOWP (w->contents))
 15818         hscrolled_p |= hscroll_window_tree (w->contents);
 15819       else if (w->cursor.vpos >= 0
 15820                /* Don't allow hscroll in mini-windows that display
 15821                   echo-area messages.  This is because desired_matrix
 15822                   of such windows was prepared while momentarily
 15823                   switched to an echo-area buffer, which is different
 15824                   from w->contents, and we simply cannot hscroll such
 15825                   windows safely.  */
 15826                && !(w == XWINDOW (echo_area_window)
 15827                     && !NILP (echo_area_buffer[0])))
 15828         {
 15829           int h_margin;
 15830           int text_area_width;
 15831           struct glyph_row *cursor_row;
 15832           struct glyph_row *bottom_row;
 15833 
 15834           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->desired_matrix, w);
 15835           if (w->cursor.vpos < bottom_row - w->desired_matrix->rows)
 15836             cursor_row = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
 15837           else
 15838             cursor_row = bottom_row - 1;
 15839 
 15840           if (!cursor_row->enabled_p)
 15841             {
 15842               bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 15843               if (w->cursor.vpos < bottom_row - w->current_matrix->rows)
 15844                 cursor_row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 15845               else
 15846                 cursor_row = bottom_row - 1;
 15847             }
 15848           bool row_r2l_p = cursor_row->reversed_p;
 15849           bool hscl = hscrolling_current_line_p (w);
 15850           int x_offset = 0;
 15851           /* When line numbers are displayed, we need to account for
 15852              the horizontal space they consume.  */
 15853           if (!NILP (Vdisplay_line_numbers))
 15854             {
 15855               struct glyph *g;
 15856               if (!row_r2l_p)
 15857                 {
 15858                   for (g = cursor_row->glyphs[TEXT_AREA];
 15859                        g < cursor_row->glyphs[TEXT_AREA]
 15860                          + cursor_row->used[TEXT_AREA];
 15861                        g++)
 15862                     {
 15863                       if (!(NILP (g->object) && g->charpos < 0))
 15864                         break;
 15865                       x_offset += g->pixel_width;
 15866                     }
 15867                 }
 15868               else
 15869                 {
 15870                   for (g = cursor_row->glyphs[TEXT_AREA]
 15871                          + cursor_row->used[TEXT_AREA];
 15872                        g > cursor_row->glyphs[TEXT_AREA];
 15873                        g--)
 15874                     {
 15875                       if (!(NILP ((g - 1)->object) && (g - 1)->charpos < 0))
 15876                         break;
 15877                       x_offset += (g - 1)->pixel_width;
 15878                     }
 15879                 }
 15880             }
 15881           if (cursor_row->truncated_on_left_p)
 15882             {
 15883               /* On TTY frames, don't count the left truncation glyph.  */
 15884               struct frame *f = XFRAME (WINDOW_FRAME (w));
 15885               x_offset -= (FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f));
 15886             }
 15887 
 15888           text_area_width = window_box_width (w, TEXT_AREA);
 15889 
 15890           /* Scroll when cursor is inside this scroll margin.  */
 15891           h_margin = (clip_to_bounds (0, hscroll_margin, 1000000)
 15892                       * WINDOW_FRAME_COLUMN_WIDTH (w));
 15893 
 15894           /* If the position of this window's point has explicitly
 15895              changed, no more suspend auto hscrolling.  */
 15896           if (w->suspend_auto_hscroll
 15897               && NILP (Fequal (Fwindow_point (window),
 15898                                Fwindow_old_point (window))))
 15899             {
 15900               w->suspend_auto_hscroll = false;
 15901               /* When hscrolling just the current line, and the rest
 15902                  of lines were temporarily hscrolled, but no longer
 15903                  are, force thorough redisplay of this window, to show
 15904                  the effect of disabling hscroll suspension immediately.  */
 15905               if (w->min_hscroll == 0 && w->hscroll > 0
 15906                   && EQ (Fbuffer_local_value (Qauto_hscroll_mode, w->contents),
 15907                          Qcurrent_line))
 15908                 SET_FRAME_GARBAGED (XFRAME (w->frame));
 15909             }
 15910 
 15911           /* Remember window point.  */
 15912           Fset_marker (w->old_pointm,
 15913                        ((w == XWINDOW (selected_window))
 15914                         ? make_fixnum (BUF_PT (XBUFFER (w->contents)))
 15915                         : Fmarker_position (w->pointm)),
 15916                        w->contents);
 15917 
 15918           if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
 15919               && !w->suspend_auto_hscroll
 15920               /* In some pathological cases, like restoring a window
 15921                  configuration into a frame that is much smaller than
 15922                  the one from which the configuration was saved, we
 15923                  get glyph rows whose start and end have zero buffer
 15924                  positions, which we cannot handle below.  Just skip
 15925                  such windows.  */
 15926               && (CHARPOS (cursor_row->start.pos)
 15927                   >= BUF_BEG (XBUFFER (w->contents)))
 15928               /* For left-to-right rows, hscroll when cursor is either
 15929                  (i) inside the right hscroll margin, or (ii) if it is
 15930                  inside the left margin and the window is already
 15931                  hscrolled.  */
 15932               && ((!row_r2l_p
 15933                    && ((w->hscroll && w->cursor.x <= h_margin + x_offset)
 15934                        || (cursor_row->enabled_p
 15935                            && cursor_row->truncated_on_right_p
 15936                            && (w->cursor.x >= text_area_width - h_margin))))
 15937                   /* For right-to-left rows, the logic is similar,
 15938                      except that rules for scrolling to left and right
 15939                      are reversed.  E.g., if cursor.x <= h_margin, we
 15940                      need to hscroll "to the right" unconditionally,
 15941                      and that will scroll the screen to the left so as
 15942                      to reveal the next portion of the row.  */
 15943                   || (row_r2l_p
 15944                       && ((cursor_row->enabled_p
 15945                            /* FIXME: It is confusing to set the
 15946                               truncated_on_right_p flag when R2L rows
 15947                               are actually truncated on the left.  */
 15948                            && cursor_row->truncated_on_right_p
 15949                            && w->cursor.x <= h_margin)
 15950                           || (w->hscroll
 15951                               && (w->cursor.x >= (text_area_width - h_margin
 15952                                                   - x_offset)))))
 15953                   /* This last condition is needed when moving
 15954                      vertically from an hscrolled line to a short line
 15955                      that doesn't need to be hscrolled.  If we omit
 15956                      this condition, the line from which we move will
 15957                      remain hscrolled.  */
 15958                   || (hscl
 15959                       && w->hscroll != w->min_hscroll
 15960                       && !cursor_row->truncated_on_left_p)))
 15961             {
 15962               struct it it;
 15963               ptrdiff_t hscroll;
 15964               struct buffer *saved_current_buffer;
 15965               ptrdiff_t pt;
 15966               int wanted_x;
 15967 
 15968               /* Find point in a display of infinite width.  */
 15969               saved_current_buffer = current_buffer;
 15970               current_buffer = XBUFFER (w->contents);
 15971 
 15972               if (w == XWINDOW (selected_window))
 15973                 pt = PT;
 15974               else
 15975                 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
 15976 
 15977               /* Move iterator to pt starting at cursor_row->start in
 15978                  a line with infinite width.  */
 15979               init_to_row_start (&it, w, cursor_row);
 15980               if (hscl)
 15981                 it.first_visible_x = window_hscroll_limited (w, it.f)
 15982                                      * FRAME_COLUMN_WIDTH (it.f);
 15983               it.last_visible_x = DISP_INFINITY;
 15984 
 15985               ptrdiff_t nchars = pt - IT_CHARPOS (it);
 15986               if (current_buffer->long_line_optimizations_p
 15987                   && nchars > large_hscroll_threshold)
 15988                 {
 15989                   /* Special optimization for very long and truncated
 15990                      lines which need to be hscrolled far to the left:
 15991                      jump directly to the (approximate) first position
 15992                      that is visible, instead of slowly walking there.  */
 15993                   fast_move_it_horizontally (&it, nchars);
 15994                   it.current_x += nchars * FRAME_COLUMN_WIDTH (it.f);
 15995                 }
 15996               else
 15997                 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
 15998               /* If the line ends in an overlay string with a newline,
 15999                  we might infloop, because displaying the window will
 16000                  want to put the cursor after the overlay, i.e. at X
 16001                  coordinate of zero on the next screen line.  So we
 16002                  use the buffer position prior to the overlay string
 16003                  instead.  */
 16004               if (it.method == GET_FROM_STRING && pt > 1)
 16005                 {
 16006                   init_to_row_start (&it, w, cursor_row);
 16007                   if (hscl)
 16008                     it.first_visible_x = (window_hscroll_limited (w, it.f)
 16009                                           * FRAME_COLUMN_WIDTH (it.f));
 16010                   if (current_buffer->long_line_optimizations_p
 16011                       && nchars > large_hscroll_threshold)
 16012                     {
 16013                       fast_move_it_horizontally (&it, nchars - 1);
 16014                       it.current_x += (nchars - 1) * FRAME_COLUMN_WIDTH (it.f);
 16015                     }
 16016                   else
 16017                     move_it_in_display_line_to (&it, pt - 1, -1, MOVE_TO_POS);
 16018                 }
 16019               current_buffer = saved_current_buffer;
 16020 
 16021               /* Position cursor in window.  */
 16022               if (!hscroll_relative_p && hscroll_step_abs == 0)
 16023                 hscroll = max (0, (it.current_x
 16024                                    - (ITERATOR_AT_END_OF_LINE_P (&it)
 16025                                       ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
 16026                                       : (text_area_width / 2))))
 16027                           / FRAME_COLUMN_WIDTH (it.f);
 16028               else if ((!row_r2l_p
 16029                         && w->cursor.x >= text_area_width - h_margin)
 16030                        || (row_r2l_p && w->cursor.x <= h_margin))
 16031                 {
 16032                   if (hscroll_relative_p)
 16033                     wanted_x = text_area_width * (1 - hscroll_step_rel)
 16034                                - h_margin;
 16035                   else
 16036                     wanted_x = text_area_width
 16037                                - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16038                                - h_margin;
 16039                   hscroll
 16040                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16041                 }
 16042               else
 16043                 {
 16044                   if (hscroll_relative_p)
 16045                     wanted_x =
 16046                       text_area_width * hscroll_step_rel + h_margin + x_offset;
 16047                   else
 16048                     wanted_x =
 16049                       hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
 16050                       + h_margin + x_offset;
 16051                   hscroll
 16052                     = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
 16053                 }
 16054               hscroll = max (hscroll, w->min_hscroll);
 16055 
 16056               /* Don't prevent redisplay optimizations if hscroll
 16057                  hasn't changed, as it will unnecessarily slow down
 16058                  redisplay.  */
 16059               if (w->hscroll != hscroll
 16060                   /* When hscrolling only the current line, we need to
 16061                      report hscroll even if its value is equal to the
 16062                      previous one, because the new line might need a
 16063                      different value.  */
 16064                   || (hscl && w->last_cursor_vpos != w->cursor.vpos))
 16065                 {
 16066                   struct buffer *b = XBUFFER (w->contents);
 16067                   b->prevent_redisplay_optimizations_p = true;
 16068                   w->hscroll = hscroll;
 16069                   hscrolled_p = true;
 16070                 }
 16071             }
 16072         }
 16073 
 16074       window = w->next;
 16075     }
 16076 
 16077   /* Value is true if hscroll of any leaf window has been changed.  */
 16078   return hscrolled_p;
 16079 }
 16080 
 16081 
 16082 /* Set hscroll so that cursor is visible and not inside horizontal
 16083    scroll margins for all windows in the tree rooted at WINDOW.  See
 16084    also hscroll_window_tree above.  Value is true if any window's
 16085    hscroll has been changed.  If it has, desired matrices on the frame
 16086    of WINDOW are cleared.  */
 16087 
 16088 static bool
 16089 hscroll_windows (Lisp_Object window)
 16090 {
 16091   bool hscrolled_p = hscroll_window_tree (window);
 16092   if (hscrolled_p)
 16093     clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
 16094   return hscrolled_p;
 16095 }
 16096 
 16097 
 16098 
 16099 /************************************************************************
 16100                                 Redisplay
 16101  ************************************************************************/
 16102 
 16103 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined.
 16104    This is sometimes handy to have in a debugger session.  */
 16105 
 16106 #ifdef GLYPH_DEBUG
 16107 
 16108 /* First and last unchanged row for try_window_id.  */
 16109 
 16110 static int debug_first_unchanged_at_end_vpos;
 16111 static int debug_last_unchanged_at_beg_vpos;
 16112 
 16113 /* Delta vpos and y.  */
 16114 
 16115 static int debug_dvpos, debug_dy;
 16116 
 16117 /* Delta in characters and bytes for try_window_id.  */
 16118 
 16119 static ptrdiff_t debug_delta, debug_delta_bytes;
 16120 
 16121 /* Values of window_end_pos and window_end_vpos at the end of
 16122    try_window_id.  */
 16123 
 16124 static ptrdiff_t debug_end_vpos;
 16125 
 16126 /* Append a string to W->desired_matrix->method.  FMT is a printf
 16127    format string.  If trace_redisplay_p is true also printf the
 16128    resulting string to stderr.  */
 16129 
 16130 static void debug_method_add (struct window *, char const *, ...)
 16131   ATTRIBUTE_FORMAT_PRINTF (2, 3);
 16132 
 16133 static void
 16134 debug_method_add (struct window *w, char const *fmt, ...)
 16135 {
 16136   void *ptr = w;
 16137   char *method = w->desired_matrix->method;
 16138   int len = strlen (method);
 16139   int size = sizeof w->desired_matrix->method;
 16140   int remaining = size - len - 1;
 16141   va_list ap;
 16142 
 16143   if (len && remaining)
 16144     {
 16145       method[len] = '|';
 16146       --remaining, ++len;
 16147     }
 16148 
 16149   va_start (ap, fmt);
 16150   vsnprintf (method + len, remaining + 1, fmt, ap);
 16151   va_end (ap);
 16152 
 16153   if (trace_redisplay_p)
 16154     fprintf (stderr, "%p (%s): %s\n",
 16155              ptr,
 16156              ((BUFFERP (w->contents)
 16157                && STRINGP (BVAR (XBUFFER (w->contents), name)))
 16158               ? SSDATA (BVAR (XBUFFER (w->contents), name))
 16159               : "no buffer"),
 16160              method + len);
 16161 }
 16162 
 16163 #endif /* GLYPH_DEBUG */
 16164 
 16165 
 16166 /* Value is true if all changes in window W, which displays
 16167    current_buffer, are in the text between START and END.  START is a
 16168    buffer position, END is given as a distance from Z.  Used in
 16169    redisplay_internal for display optimization.  */
 16170 
 16171 static bool
 16172 text_outside_line_unchanged_p (struct window *w,
 16173                                ptrdiff_t start, ptrdiff_t end)
 16174 {
 16175   bool unchanged_p = true;
 16176 
 16177   /* If text or overlays have changed, see where.  */
 16178   if (window_outdated (w))
 16179     {
 16180       /* Gap in the line?  */
 16181       if (GPT < start || Z - GPT < end)
 16182         unchanged_p = false;
 16183 
 16184       /* Changes start in front of the line, or end after it?  */
 16185       if (unchanged_p
 16186           && (BEG_UNCHANGED < start - 1
 16187               || END_UNCHANGED < end))
 16188         unchanged_p = false;
 16189 
 16190       /* If selective display, can't optimize if changes start at the
 16191          beginning of the line.  */
 16192       if (unchanged_p
 16193           && FIXNUMP (BVAR (current_buffer, selective_display))
 16194           && XFIXNUM (BVAR (current_buffer, selective_display)) > 0
 16195           && (BEG_UNCHANGED < start || GPT <= start))
 16196         unchanged_p = false;
 16197 
 16198       /* If there are overlays at the start or end of the line, these
 16199          may have overlay strings with newlines in them.  A change at
 16200          START, for instance, may actually concern the display of such
 16201          overlay strings as well, and they are displayed on different
 16202          lines.  So, quickly rule out this case.  (For the future, it
 16203          might be desirable to implement something more telling than
 16204          just BEG/END_UNCHANGED.)  */
 16205       if (unchanged_p)
 16206         {
 16207           if (BEG + BEG_UNCHANGED == start
 16208               && overlay_touches_p (start))
 16209             unchanged_p = false;
 16210           if (END_UNCHANGED == end
 16211               && overlay_touches_p (Z - end))
 16212             unchanged_p = false;
 16213         }
 16214 
 16215       /* Under bidi reordering, adding or deleting a character in the
 16216          beginning of a paragraph, before the first strong directional
 16217          character, can change the base direction of the paragraph (unless
 16218          the buffer specifies a fixed paragraph direction), which will
 16219          require redisplaying the whole paragraph.  It might be worthwhile
 16220          to find the paragraph limits and widen the range of redisplayed
 16221          lines to that, but for now just give up this optimization.  */
 16222       if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 16223           && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 16224         unchanged_p = false;
 16225     }
 16226 
 16227   return unchanged_p;
 16228 }
 16229 
 16230 
 16231 /* Do a frame update, taking possible shortcuts into account.  This is
 16232    the main external entry point for redisplay.
 16233 
 16234    If the last redisplay displayed an echo area message and that message
 16235    is no longer requested, we clear the echo area or bring back the
 16236    mini-buffer if that is in use.  */
 16237 
 16238 void
 16239 redisplay (void)
 16240 {
 16241   redisplay_internal ();
 16242 }
 16243 
 16244 
 16245 static Lisp_Object
 16246 overlay_arrow_string_or_property (Lisp_Object var)
 16247 {
 16248   Lisp_Object val;
 16249 
 16250   if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
 16251     return val;
 16252 
 16253   return Voverlay_arrow_string;
 16254 }
 16255 
 16256 /* Return true if there are any overlay-arrows in current_buffer.  */
 16257 static bool
 16258 overlay_arrow_in_current_buffer_p (void)
 16259 {
 16260   Lisp_Object vlist;
 16261 
 16262   for (vlist = Voverlay_arrow_variable_list;
 16263        CONSP (vlist);
 16264        vlist = XCDR (vlist))
 16265     {
 16266       Lisp_Object var = XCAR (vlist);
 16267       Lisp_Object val;
 16268 
 16269       if (!SYMBOLP (var))
 16270         continue;
 16271       val = find_symbol_value (var);
 16272       if (MARKERP (val)
 16273           && current_buffer == XMARKER (val)->buffer)
 16274         return true;
 16275     }
 16276   return false;
 16277 }
 16278 
 16279 
 16280 /* Return true if any overlay_arrows have moved or overlay-arrow-string
 16281    has changed.
 16282    If SET_REDISPLAY is true, additionally, set the `redisplay' bit in those
 16283    buffers that are affected.  */
 16284 
 16285 static bool
 16286 overlay_arrows_changed_p (bool set_redisplay)
 16287 {
 16288   Lisp_Object vlist;
 16289   bool changed = false;
 16290 
 16291   for (vlist = Voverlay_arrow_variable_list;
 16292        CONSP (vlist);
 16293        vlist = XCDR (vlist))
 16294     {
 16295       Lisp_Object var = XCAR (vlist);
 16296       Lisp_Object val, pstr;
 16297 
 16298       if (!SYMBOLP (var))
 16299         continue;
 16300       val = find_symbol_value (var);
 16301       if (!MARKERP (val))
 16302         continue;
 16303       if (! EQ (Fmarker_position (val),
 16304                 /* FIXME: Don't we have a problem, using such a global
 16305                  * "last-position" if the variable is buffer-local?  */
 16306                 Fget (var, Qlast_arrow_position))
 16307           || ! (pstr = overlay_arrow_string_or_property (var),
 16308                 EQ (pstr, Fget (var, Qlast_arrow_string))))
 16309         {
 16310           struct buffer *buf = XMARKER (val)->buffer;
 16311 
 16312           if (set_redisplay)
 16313             {
 16314               if (buf)
 16315                 bset_redisplay (buf);
 16316               changed = true;
 16317             }
 16318           else
 16319             return true;
 16320         }
 16321     }
 16322   return changed;
 16323 }
 16324 
 16325 /* Mark overlay arrows to be updated on next redisplay.  */
 16326 
 16327 static void
 16328 update_overlay_arrows (int up_to_date)
 16329 {
 16330   Lisp_Object vlist;
 16331 
 16332   for (vlist = Voverlay_arrow_variable_list;
 16333        CONSP (vlist);
 16334        vlist = XCDR (vlist))
 16335     {
 16336       Lisp_Object var = XCAR (vlist);
 16337 
 16338       if (!SYMBOLP (var))
 16339         continue;
 16340 
 16341       if (up_to_date > 0)
 16342         {
 16343           Lisp_Object val = find_symbol_value (var);
 16344           if (!MARKERP (val))
 16345             continue;
 16346           Fput (var, Qlast_arrow_position, Fmarker_position (val));
 16347           Fput (var, Qlast_arrow_string,
 16348                 overlay_arrow_string_or_property (var));
 16349         }
 16350       else if (up_to_date < 0
 16351                || !NILP (Fget (var, Qlast_arrow_position)))
 16352         {
 16353           Fput (var, Qlast_arrow_position, Qt);
 16354           Fput (var, Qlast_arrow_string, Qt);
 16355         }
 16356     }
 16357 }
 16358 
 16359 
 16360 /* Return overlay arrow string to display at row.
 16361    Return integer (bitmap number) for arrow bitmap in left fringe.
 16362    Return nil if no overlay arrow.  */
 16363 
 16364 static Lisp_Object
 16365 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
 16366 {
 16367   Lisp_Object vlist;
 16368 
 16369   for (vlist = Voverlay_arrow_variable_list;
 16370        CONSP (vlist);
 16371        vlist = XCDR (vlist))
 16372     {
 16373       Lisp_Object var = XCAR (vlist);
 16374       Lisp_Object val;
 16375 
 16376       if (!SYMBOLP (var))
 16377         continue;
 16378 
 16379       val = find_symbol_value (var);
 16380 
 16381       if (MARKERP (val)
 16382           && current_buffer == XMARKER (val)->buffer
 16383           && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
 16384         {
 16385           if (FRAME_WINDOW_P (it->f)
 16386               /* FIXME: if ROW->reversed_p is set, this should test
 16387                  the right fringe, not the left one.  */
 16388               && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
 16389             {
 16390 #ifdef HAVE_WINDOW_SYSTEM
 16391               if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
 16392                 {
 16393                   int fringe_bitmap = lookup_fringe_bitmap (val);
 16394                   if (fringe_bitmap != 0)
 16395                     return make_fixnum (fringe_bitmap);
 16396                 }
 16397 #endif
 16398               return make_fixnum (-1); /* Use default arrow bitmap.  */
 16399             }
 16400           return overlay_arrow_string_or_property (var);
 16401         }
 16402     }
 16403 
 16404   return Qnil;
 16405 }
 16406 
 16407 /* Return true if point moved out of or into a composition.  Otherwise
 16408    return false.  PREV_BUF and PREV_PT are the last point buffer and
 16409    position.  BUF and PT are the current point buffer and position.  */
 16410 
 16411 static bool
 16412 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
 16413                             struct buffer *buf, ptrdiff_t pt)
 16414 {
 16415   ptrdiff_t start, end;
 16416   Lisp_Object prop;
 16417   Lisp_Object buffer;
 16418 
 16419   XSETBUFFER (buffer, buf);
 16420   /* Check a composition at the last point if point moved within the
 16421      same buffer.  */
 16422   if (prev_buf == buf)
 16423     {
 16424       if (prev_pt == pt)
 16425         /* Point didn't move.  */
 16426         return false;
 16427 
 16428       if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
 16429           && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
 16430           && composition_valid_p (start, end, prop)
 16431           && start < prev_pt && end > prev_pt)
 16432         /* The last point was within the composition.  Return true iff
 16433             point moved out of the composition.  */
 16434         return (pt <= start || pt >= end);
 16435     }
 16436 
 16437   /* Check a composition at the current point.  */
 16438   return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
 16439           && find_composition (pt, -1, &start, &end, &prop, buffer)
 16440           && composition_valid_p (start, end, prop)
 16441           && start < pt && end > pt);
 16442 }
 16443 
 16444 /* Reconsider the clip changes of buffer which is displayed in W.  */
 16445 
 16446 static void
 16447 reconsider_clip_changes (struct window *w)
 16448 {
 16449   struct buffer *b = XBUFFER (w->contents);
 16450 
 16451   if (b->clip_changed
 16452       && w->window_end_valid
 16453       && w->current_matrix->buffer == b
 16454       && w->current_matrix->zv == BUF_ZV (b)
 16455       && w->current_matrix->begv == BUF_BEGV (b))
 16456     b->clip_changed = false;
 16457 
 16458   /* If display wasn't paused, and W is not a tool bar window, see if
 16459      point has been moved into or out of a composition.  In that case,
 16460      set b->clip_changed to force updating the screen.  If
 16461      b->clip_changed has already been set, skip this check.  */
 16462   if (!b->clip_changed && w->window_end_valid)
 16463     {
 16464       ptrdiff_t pt = (w == XWINDOW (selected_window)
 16465                       ? PT : marker_position (w->pointm));
 16466 
 16467       if ((w->current_matrix->buffer != b || pt != w->last_point)
 16468           && check_point_in_composition (w->current_matrix->buffer,
 16469                                          w->last_point, b, pt))
 16470         b->clip_changed = true;
 16471     }
 16472 }
 16473 
 16474 static void
 16475 propagate_buffer_redisplay (void)
 16476 { /* Resetting b->text->redisplay is problematic!
 16477      We can't just reset it in the case that some window that displays
 16478      it has not been redisplayed; and such a window can stay
 16479      unredisplayed for a long time if it's currently invisible.
 16480      But we do want to reset it at the end of redisplay otherwise
 16481      its displayed windows will keep being redisplayed over and over
 16482      again.
 16483      So we copy all b->text->redisplay flags up to their windows here,
 16484      such that mark_window_display_accurate can safely reset
 16485      b->text->redisplay.  */
 16486   Lisp_Object ws = window_list ();
 16487   for (; CONSP (ws); ws = XCDR (ws))
 16488     {
 16489       struct window *thisw = XWINDOW (XCAR (ws));
 16490       struct buffer *thisb = XBUFFER (thisw->contents);
 16491       if (thisb->text->redisplay)
 16492         thisw->redisplay = true;
 16493     }
 16494 }
 16495 
 16496 #define STOP_POLLING                                    \
 16497 do { if (! polling_stopped_here) stop_polling ();       \
 16498        polling_stopped_here = true; } while (false)
 16499 
 16500 #define RESUME_POLLING                                  \
 16501 do { if (polling_stopped_here) start_polling ();        \
 16502        polling_stopped_here = false; } while (false)
 16503 
 16504 /* Perhaps in the future avoid recentering windows if it
 16505    is not necessary; currently that causes some problems.  */
 16506 
 16507 static void
 16508 redisplay_internal (void)
 16509 {
 16510   struct window *w = XWINDOW (selected_window);
 16511   struct window *sw;
 16512   struct frame *fr;
 16513   bool pending;
 16514   bool must_finish = false, match_p;
 16515   struct text_pos tlbufpos, tlendpos;
 16516   int number_of_visible_frames;
 16517   struct frame *sf;
 16518   bool polling_stopped_here = false;
 16519   Lisp_Object tail, frame;
 16520 
 16521   /* Set a limit to the number of retries we perform due to horizontal
 16522      scrolling, this avoids getting stuck in an uninterruptible
 16523      infinite loop (Bug #24633).  */
 16524   enum { MAX_HSCROLL_RETRIES = 16 };
 16525   int hscroll_retries = 0;
 16526 
 16527   /* Limit the number of retries for when frame(s) become garbaged as
 16528      result of redisplaying them.  Some packages set various redisplay
 16529      hooks, such as window-scroll-functions, to run Lisp that always
 16530      calls APIs which cause the frame's garbaged flag to become set,
 16531      so we loop indefinitely.  */
 16532   enum {MAX_GARBAGED_FRAME_RETRIES = 2 };
 16533   int garbaged_frame_retries = 0;
 16534 
 16535   /* False means that only the selected_window needs to be updated.
 16536      True means that other windows may need to be updated as well,
 16537      so we need to consult `needs_no_update` for all windows.  */
 16538   bool consider_all_windows_p;
 16539 
 16540   /* True means redisplay has to redisplay the miniwindow.  */
 16541   bool update_miniwindow_p = false;
 16542 
 16543   redisplay_trace ("redisplay_internal %d\n", redisplaying_p);
 16544 
 16545   /* No redisplay if running in batch mode or frame is not yet fully
 16546      initialized, or redisplay is explicitly turned off by setting
 16547      Vinhibit_redisplay.  */
 16548   if ((FRAME_INITIAL_P (SELECTED_FRAME ())
 16549        && redisplay_skip_initial_frame)
 16550       || !NILP (Vinhibit_redisplay))
 16551     return;
 16552 
 16553   /* Don't examine these until after testing Vinhibit_redisplay.
 16554      When Emacs is shutting down, perhaps because its connection to
 16555      X has dropped, we should not look at them at all.  */
 16556   fr = XFRAME (w->frame);
 16557   sf = SELECTED_FRAME ();
 16558 
 16559   if (!fr->glyphs_initialized_p)
 16560     return;
 16561 
 16562 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
 16563   if (popup_activated ())
 16564     return;
 16565 #endif
 16566 
 16567 #if defined (HAVE_HAIKU)
 16568   if (popup_activated_p)
 16569     return;
 16570 #endif
 16571 
 16572   /* I don't think this happens but let's be paranoid.  */
 16573   if (redisplaying_p)
 16574     return;
 16575 
 16576   /* Record a function that clears redisplaying_p
 16577      when we leave this function.  */
 16578   specpdl_ref count = SPECPDL_INDEX ();
 16579   record_unwind_protect_void (unwind_redisplay);
 16580   redisplaying_p = true;
 16581   block_buffer_flips ();
 16582   specbind (Qinhibit_free_realized_faces, Qnil);
 16583 
 16584   /* Record this function, so it appears on the profiler's backtraces.  */
 16585   record_in_backtrace (Qredisplay_internal_xC_functionx, 0, 0);
 16586 
 16587   FOR_EACH_FRAME (tail, frame)
 16588     XFRAME (frame)->already_hscrolled_p = false;
 16589 
 16590   reset_outermost_restrictions ();
 16591 
 16592  retry:
 16593   /* Remember the currently selected window.  */
 16594   sw = w;
 16595 
 16596   pending = false;
 16597   forget_escape_and_glyphless_faces ();
 16598 
 16599   inhibit_free_realized_faces = false;
 16600 
 16601   /* If face_change, init_iterator will free all realized faces, which
 16602      includes the faces referenced from current matrices.  So, we
 16603      can't reuse current matrices in this case.  */
 16604   if (face_change)
 16605     windows_or_buffers_changed = 47;
 16606 
 16607   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
 16608       && FRAME_TTY (sf)->previous_frame != sf)
 16609     {
 16610       /* Since frames on a single ASCII terminal share the same
 16611          display area, displaying a different frame means redisplay
 16612          the whole thing.  */
 16613       SET_FRAME_GARBAGED (sf);
 16614 #ifndef DOS_NT
 16615       set_tty_color_mode (FRAME_TTY (sf), sf);
 16616 #endif
 16617       FRAME_TTY (sf)->previous_frame = sf;
 16618     }
 16619 
 16620   /* Set the visible flags for all frames.  Do this before checking for
 16621      resized or garbaged frames; they want to know if their frames are
 16622      visible.  See the comment in frame.h for FRAME_SAMPLE_VISIBILITY.  */
 16623   number_of_visible_frames = 0;
 16624 
 16625   FOR_EACH_FRAME (tail, frame)
 16626     {
 16627       struct frame *f = XFRAME (frame);
 16628 
 16629       if (FRAME_REDISPLAY_P (f))
 16630         {
 16631           ++number_of_visible_frames;
 16632           /* Adjust matrices for visible frames only.  */
 16633           if (f->fonts_changed)
 16634             {
 16635               adjust_frame_glyphs (f);
 16636               /* Disable all redisplay optimizations for this frame.
 16637                  This is because adjust_frame_glyphs resets the
 16638                  enabled_p flag for all glyph rows of all windows, so
 16639                  many optimizations will fail anyway, and some might
 16640                  fail to test that flag and do bogus things as
 16641                  result.  */
 16642               SET_FRAME_GARBAGED (f);
 16643               f->fonts_changed = false;
 16644             }
 16645           /* If cursor type has been changed on the frame
 16646              other than selected, consider all frames.  */
 16647           if (f != sf && f->cursor_type_changed)
 16648             fset_redisplay (f);
 16649         }
 16650       clear_desired_matrices (f);
 16651     }
 16652 
 16653   /* Notice any pending interrupt request to change frame size.  */
 16654   do_pending_window_change (true);
 16655 
 16656   /* Clear frames marked as garbaged.  */
 16657   clear_garbaged_frames ();
 16658 
 16659   /* Build menubar and tool-bar items.  */
 16660   if (NILP (Vmemory_full))
 16661     prepare_menu_bars ();
 16662 
 16663   /* do_pending_window_change could change the selected_window due to
 16664      frame resizing which makes the selected window too small.
 16665      prepare_menu_bars may call lisp hooks and hence also change the
 16666      selected_window.  */
 16667   if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
 16668     sw = w;
 16669 
 16670   reconsider_clip_changes (w);
 16671 
 16672   /* In most cases selected window displays current buffer.  */
 16673   match_p = XBUFFER (w->contents) == current_buffer;
 16674   if (match_p)
 16675     {
 16676       /* Detect case that we need to write or remove a star in the mode line.  */
 16677       if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
 16678         w->update_mode_line = true;
 16679 
 16680       if (mode_line_update_needed (w))
 16681         w->update_mode_line = true;
 16682 
 16683       /* If reconsider_clip_changes above decided that the narrowing
 16684          in the current buffer changed, make sure all other windows
 16685          showing that buffer will be redisplayed.  */
 16686       if (current_buffer->clip_changed)
 16687         bset_update_mode_line (current_buffer);
 16688     }
 16689 
 16690   /* Normally the message* functions will have already displayed and
 16691      updated the echo area, but the frame may have been trashed, or
 16692      the update may have been preempted, so display the echo area
 16693      again here.  Checking message_cleared_p captures the case that
 16694      the echo area should be cleared.  */
 16695   if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
 16696       || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
 16697       || (message_cleared_p
 16698           && minibuf_level == 0
 16699           /* If the mini-window is currently selected, this means the
 16700              echo-area doesn't show through.  */
 16701           && !MINI_WINDOW_P (XWINDOW (selected_window))))
 16702     {
 16703       echo_area_display (false);
 16704 
 16705       if (message_cleared_p)
 16706         update_miniwindow_p = true;
 16707 
 16708       must_finish = true;
 16709 
 16710       /* If we don't display the current message, don't clear the
 16711          message_cleared_p flag, because, if we did, we wouldn't clear
 16712          the echo area in the next redisplay which doesn't preserve
 16713          the echo area.  */
 16714       if (!display_last_displayed_message_p)
 16715         message_cleared_p = false;
 16716     }
 16717   else if (EQ (selected_window, minibuf_window)
 16718            && (current_buffer->clip_changed || window_outdated (w))
 16719            && resize_mini_window (w, false))
 16720     {
 16721       /* Resized active mini-window to fit the size of what it is
 16722          showing if its contents might have changed.  */
 16723       must_finish = true;
 16724 
 16725       /* If window configuration was changed, frames may have been
 16726          marked garbaged.  Clear them or we will experience
 16727          surprises wrt scrolling.  */
 16728       clear_garbaged_frames ();
 16729     }
 16730 
 16731   if (!NILP (Vrun_hooks))
 16732     run_window_change_functions ();
 16733 
 16734   if (windows_or_buffers_changed && !update_mode_lines)
 16735     /* Code that sets windows_or_buffers_changed doesn't distinguish whether
 16736        only the windows's contents needs to be refreshed, or whether the
 16737        mode-lines also need a refresh.  */
 16738     update_mode_lines = (windows_or_buffers_changed == REDISPLAY_SOME
 16739                          ? REDISPLAY_SOME : 32);
 16740 
 16741   /* If specs for an arrow have changed, do thorough redisplay
 16742      to ensure we remove any arrow that should no longer exist.  */
 16743   /* Apparently, this is the only case where we update other windows,
 16744      without updating other mode-lines.  */
 16745   overlay_arrows_changed_p (true);
 16746 
 16747   consider_all_windows_p = (update_mode_lines
 16748                             || windows_or_buffers_changed);
 16749 
 16750 #define AINC(a,i)                                                       \
 16751   {                                                                     \
 16752     Lisp_Object entry = Fgethash (make_fixnum (i), a, make_fixnum (0)); \
 16753     if (FIXNUMP (entry))                                                \
 16754       Fputhash (make_fixnum (i), make_fixnum (1 + XFIXNUM (entry)), a); \
 16755   }
 16756 
 16757   AINC (Vredisplay__all_windows_cause, windows_or_buffers_changed);
 16758   AINC (Vredisplay__mode_lines_cause, update_mode_lines);
 16759 
 16760   /* Optimize the case that only the line containing the cursor in the
 16761      selected window has changed.  Variables starting with this_ are
 16762      set in display_line and record information about the line
 16763      containing the cursor.  */
 16764   tlbufpos = this_line_start_pos;
 16765   tlendpos = this_line_end_pos;
 16766   if (!consider_all_windows_p
 16767       && CHARPOS (tlbufpos) > 0
 16768       && !w->update_mode_line
 16769       && !current_buffer->clip_changed
 16770       && !current_buffer->prevent_redisplay_optimizations_p
 16771       && FRAME_REDISPLAY_P (XFRAME (w->frame))
 16772       && !FRAME_OBSCURED_P (XFRAME (w->frame))
 16773       && !XFRAME (w->frame)->cursor_type_changed
 16774       && !XFRAME (w->frame)->face_change
 16775       /* Make sure recorded data applies to current buffer, etc.  */
 16776       && this_line_buffer == current_buffer
 16777       && match_p
 16778       && !w->force_start
 16779       && !w->optional_new_start
 16780       /* Point must be on the line that we have info recorded about.  */
 16781       && PT >= CHARPOS (tlbufpos)
 16782       && PT <= Z - CHARPOS (tlendpos)
 16783       /* FIXME: The following condition is only needed when
 16784          significant parts of the buffer are hidden (e.g., under
 16785          hs-minor-mode), but there doesn't seem to be a simple way of
 16786          detecting that, so we always disable the one-line redisplay
 16787          optimizations whenever display-line-numbers-mode is turned on
 16788          in the buffer.  */
 16789       && (NILP (Vdisplay_line_numbers)
 16790           || EQ (Vdisplay_line_numbers, Qvisual))
 16791       /* All text outside that line, including its final newline,
 16792          must be unchanged.  */
 16793       && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
 16794                                         CHARPOS (tlendpos)))
 16795     {
 16796       if (CHARPOS (tlbufpos) > BEGV
 16797           && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
 16798           && (CHARPOS (tlbufpos) == ZV
 16799               || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
 16800         /* Former continuation line has disappeared by becoming empty.  */
 16801         goto cancel;
 16802       else if (window_outdated (w) || MINI_WINDOW_P (w))
 16803         {
 16804           /* We have to handle the case of continuation around a
 16805              wide-column character (see the comment in indent.c around
 16806              line 1340).
 16807 
 16808              For instance, in the following case:
 16809 
 16810              --------  Insert  --------
 16811              K_A_N_\\   `a'    K_A_N_a\         `X_' are wide-column chars.
 16812              J_I_       ==>    J_I_             `^^' are cursors.
 16813              ^^                ^^
 16814              --------          --------
 16815 
 16816              As we have to redraw the line above, we cannot use this
 16817              optimization.  */
 16818 
 16819           struct it it;
 16820           int line_height_before = this_line_pixel_height;
 16821 
 16822           /* Note that start_display will handle the case that the
 16823              line starting at tlbufpos is a continuation line.  */
 16824           start_display (&it, w, tlbufpos);
 16825 
 16826           /* Implementation note: It this still necessary?  */
 16827           if (it.current_x != this_line_start_x)
 16828             goto cancel;
 16829 
 16830           /* Give up on this optimization if the line starts with a
 16831              string with display property that draws on the fringes,
 16832              as that might interfere with line-prefix display.  */
 16833           if (it.sp > 1
 16834               && it.method == GET_FROM_IMAGE && it.image_id == -1)
 16835             goto cancel;
 16836           redisplay_trace ("trying display optimization 1\n");
 16837           w->cursor.vpos = -1;
 16838           overlay_arrow_seen = false;
 16839           it.vpos = this_line_vpos;
 16840           it.current_y = this_line_y;
 16841           it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
 16842           display_line (&it, -1);
 16843 
 16844           /* If line contains point, is not continued,
 16845              and ends at same distance from eob as before, we win.  */
 16846           if (w->cursor.vpos >= 0
 16847               /* Line is not continued, otherwise this_line_start_pos
 16848                  would have been set to 0 in display_line.  */
 16849               && CHARPOS (this_line_start_pos)
 16850               /* Line ends as before.  */
 16851               && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
 16852               /* Line has same height as before.  Otherwise other lines
 16853                  would have to be shifted up or down.  */
 16854               && this_line_pixel_height == line_height_before
 16855               /* Cannot use this optimization if hscrolling current
 16856                  line and this line is the current one, because
 16857                  display_line above is not informed about the
 16858                  current-line's vpos, and cannot DTRT in that case.  */
 16859               && !hscrolling_current_line_p (w))
 16860             {
 16861               /* If this is not the window's last line, we must adjust
 16862                  the charstarts of the lines below.  */
 16863               if (it.current_y < it.last_visible_y)
 16864                 {
 16865                   struct glyph_row *row
 16866                     = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
 16867                   ptrdiff_t delta, delta_bytes;
 16868 
 16869                   /* We used to distinguish between two cases here,
 16870                      conditioned by Z - CHARPOS (tlendpos) == ZV, for
 16871                      when the line ends in a newline or the end of the
 16872                      buffer's accessible portion.  But both cases did
 16873                      the same, so they were collapsed.  */
 16874                   delta = (Z
 16875                            - CHARPOS (tlendpos)
 16876                            - MATRIX_ROW_START_CHARPOS (row));
 16877                   delta_bytes = (Z_BYTE
 16878                                  - BYTEPOS (tlendpos)
 16879                                  - MATRIX_ROW_START_BYTEPOS (row));
 16880 
 16881                   increment_matrix_positions (w->current_matrix,
 16882                                               this_line_vpos + 1,
 16883                                               w->current_matrix->nrows,
 16884                                               delta, delta_bytes);
 16885                 }
 16886 
 16887               /* If this row displays text now but previously didn't,
 16888                  or vice versa, w->window_end_vpos may have to be
 16889                  adjusted.  */
 16890               if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
 16891                 {
 16892                   if (w->window_end_vpos < this_line_vpos)
 16893                     w->window_end_vpos = this_line_vpos;
 16894                 }
 16895               else if (w->window_end_vpos == this_line_vpos
 16896                        && this_line_vpos > 0)
 16897                 w->window_end_vpos = this_line_vpos - 1;
 16898               w->window_end_valid = false;
 16899 
 16900               /* Update hint: No need to try to scroll in update_window.  */
 16901               w->desired_matrix->no_scrolling_p = true;
 16902 
 16903 #ifdef GLYPH_DEBUG
 16904               *w->desired_matrix->method = 0;
 16905               debug_method_add (w, "optimization 1");
 16906 #endif
 16907 #ifdef HAVE_WINDOW_SYSTEM
 16908               update_window_fringes (w, false);
 16909 #endif
 16910               goto update;
 16911             }
 16912           else
 16913             goto cancel;
 16914         }
 16915       else if (/* Cursor position hasn't changed.  */
 16916                PT == w->last_point
 16917                /* Make sure the cursor was last displayed
 16918                   in this window.  Otherwise we have to reposition it.  */
 16919 
 16920                /* PXW: Must be converted to pixels, probably.  */
 16921                && 0 <= w->cursor.vpos
 16922                && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
 16923         {
 16924           if (!must_finish)
 16925             {
 16926               do_pending_window_change (true);
 16927               /* If selected_window changed, redisplay again.  */
 16928               if (WINDOWP (selected_window)
 16929                   && (w = XWINDOW (selected_window)) != sw)
 16930                 goto retry;
 16931 
 16932                 /* We used to always goto end_of_redisplay here, but this
 16933                  isn't enough if we have a blinking cursor.  */
 16934               if (w->cursor_off_p == w->last_cursor_off_p)
 16935                 goto end_of_redisplay;
 16936             }
 16937           goto update;
 16938         }
 16939       /* If highlighting the region, or if the cursor is in the echo area,
 16940          then we can't just move the cursor.  */
 16941       else if (NILP (Vshow_trailing_whitespace)
 16942                && !cursor_in_echo_area
 16943                && !composition_break_at_point)
 16944         {
 16945           struct it it;
 16946           struct glyph_row *row;
 16947 
 16948           /* Skip from tlbufpos to PT and see where it is.  Note that
 16949              PT may be in invisible text.  If so, we will end at the
 16950              next visible position.  */
 16951           init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
 16952                          NULL, DEFAULT_FACE_ID);
 16953           it.current_x = this_line_start_x;
 16954           it.current_y = this_line_y;
 16955           it.vpos = this_line_vpos;
 16956 
 16957           if (current_buffer->long_line_optimizations_p
 16958               && it.line_wrap == TRUNCATE
 16959               && PT - CHARPOS (tlbufpos) > large_hscroll_threshold)
 16960             {
 16961               /* When lines are very long and truncated, jumping to
 16962                  the next visible line is much faster than slowly
 16963                  iterating there.  */
 16964               reseat_at_next_visible_line_start (&it, false);
 16965               if (IT_CHARPOS (it) <= PT) /* point moved off this line */
 16966                 it.vpos = this_line_vpos + 1;
 16967             }
 16968           else
 16969             {
 16970               /* The call to move_it_to stops in front of PT, but
 16971                  moves over before-strings.  */
 16972               move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
 16973             }
 16974 
 16975           if (it.vpos == this_line_vpos
 16976               && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
 16977                   row->enabled_p))
 16978             {
 16979               eassert (this_line_vpos == it.vpos);
 16980               eassert (this_line_y == it.current_y);
 16981               set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 16982               if (cursor_row_fully_visible_p (w, false, true, false))
 16983                 {
 16984 #ifdef GLYPH_DEBUG
 16985                   *w->desired_matrix->method = 0;
 16986                   debug_method_add (w, "optimization 3");
 16987 #endif
 16988                   goto update;
 16989                 }
 16990               else
 16991                 goto cancel;
 16992             }
 16993           else
 16994             goto cancel;
 16995         }
 16996 
 16997     cancel:
 16998       /* Text changed drastically or point moved off of line.  */
 16999       SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, false);
 17000     }
 17001 
 17002   CHARPOS (this_line_start_pos) = 0;
 17003   ++clear_face_cache_count;
 17004 #ifdef HAVE_WINDOW_SYSTEM
 17005   ++clear_image_cache_count;
 17006 #endif
 17007 
 17008   /* Build desired matrices, and update the display.  If
 17009      consider_all_windows_p, do it for all windows on all frames that
 17010      require redisplay, as specified by their 'redisplay' flag.
 17011      Otherwise do it for selected_window, only.  */
 17012 
 17013   if (consider_all_windows_p)
 17014     {
 17015       FOR_EACH_FRAME (tail, frame)
 17016         XFRAME (frame)->updated_p = false;
 17017 
 17018       propagate_buffer_redisplay ();
 17019 
 17020       FOR_EACH_FRAME (tail, frame)
 17021         {
 17022           struct frame *f = XFRAME (frame);
 17023 
 17024           /* We don't have to do anything for unselected terminal
 17025              frames.  */
 17026           if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
 17027               && !EQ (FRAME_TTY (f)->top_frame, frame))
 17028             continue;
 17029 
 17030         retry_frame:
 17031           if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
 17032             {
 17033               bool gcscrollbars
 17034                 /* Only GC scrollbars when we redisplay the whole frame.  */
 17035                 = f->redisplay || !REDISPLAY_SOME_P ();
 17036               bool f_redisplay_flag = f->redisplay;
 17037 
 17038               /* The X error handler may have deleted that frame
 17039                  before we went back to retry_frame.  This must come
 17040                  before any accesses to f->terminal.  */
 17041               if (!FRAME_LIVE_P (f))
 17042                 continue;
 17043 
 17044               /* Mark all the scroll bars to be removed; we'll redeem
 17045                  the ones we want when we redisplay their windows.  */
 17046               if (gcscrollbars && FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
 17047                 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
 17048 
 17049               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17050                 {
 17051                   /* Don't allow freeing images and faces for this
 17052                      frame as long as the frame's update wasn't
 17053                      completed.  This prevents crashes when some Lisp
 17054                      that runs from the various hooks or font-lock
 17055                      decides to clear the frame's image cache and face
 17056                      cache, when the images and faces in those caches
 17057                      are referenced by the desired matrix.  */
 17058                   f->inhibit_clear_image_cache = true;
 17059                   redisplay_windows (FRAME_ROOT_WINDOW (f));
 17060                 }
 17061               /* Remember that the invisible frames need to be redisplayed next
 17062                  time they're visible.  */
 17063               else if (!REDISPLAY_SOME_P ())
 17064                 f->redisplay = true;
 17065 
 17066               /* The X error handler may have deleted that frame.  */
 17067               if (!FRAME_LIVE_P (f))
 17068                 continue;
 17069 
 17070               /* Any scroll bars which redisplay_windows should have
 17071                  nuked should now go away.  */
 17072               if (gcscrollbars && FRAME_TERMINAL (f)->judge_scroll_bars_hook)
 17073                 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
 17074 
 17075               if (FRAME_REDISPLAY_P (f) && !FRAME_OBSCURED_P (f))
 17076                 {
 17077                   /* If fonts changed on visible frame, display again.  */
 17078                   if (f->fonts_changed)
 17079                     {
 17080                       adjust_frame_glyphs (f);
 17081                       /* Disable all redisplay optimizations for this
 17082                          frame.  For the reasons, see the comment near
 17083                          the previous call to adjust_frame_glyphs above.  */
 17084                       SET_FRAME_GARBAGED (f);
 17085                       f->fonts_changed = false;
 17086                       goto retry_frame;
 17087                     }
 17088 
 17089                   /* See if we have to hscroll.  */
 17090                   if (!f->already_hscrolled_p)
 17091                     {
 17092                       f->already_hscrolled_p = true;
 17093                       if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17094                           && hscroll_windows (f->root_window))
 17095                         {
 17096                           hscroll_retries++;
 17097                           goto retry_frame;
 17098                         }
 17099                     }
 17100 
 17101                   /* If the frame's redisplay flag was not set before
 17102                      we went about redisplaying its windows, but it is
 17103                      set now, that means we employed some redisplay
 17104                      optimizations inside redisplay_windows, and
 17105                      bypassed producing some screen lines.  But if
 17106                      f->redisplay is now set, it might mean the old
 17107                      faces are no longer valid (e.g., if redisplaying
 17108                      some window called some Lisp which defined a new
 17109                      face or redefined an existing face), so trying to
 17110                      use them in update_frame will segfault.
 17111                      Therefore, we must redisplay this frame.  */
 17112                   if (!f_redisplay_flag && f->redisplay)
 17113                     goto retry_frame;
 17114                   /* In some case (e.g., window resize), we notice
 17115                      only during window updating that the window
 17116                      content changed unpredictably (e.g., a GTK
 17117                      scrollbar moved, or some Lisp hook that winds up
 17118                      calling adjust_frame_glyphs) and that our
 17119                      previous estimation of the frame content was
 17120                      garbage.  We have to start over.  These cases
 17121                      should be rare, so going all the way back to the
 17122                      top of redisplay should be good enough.  */
 17123                   if (FRAME_GARBAGED_P (f)
 17124                       && garbaged_frame_retries++ < MAX_GARBAGED_FRAME_RETRIES)
 17125                     goto retry;
 17126 
 17127 #ifdef HAVE_WINDOW_SYSTEM
 17128                   if (FRAME_WINDOW_P (f)
 17129                       && FRAME_RIF (f)->clear_under_internal_border)
 17130                     FRAME_RIF (f)->clear_under_internal_border (f);
 17131 #endif
 17132                   /* Prevent various kinds of signals during display
 17133                      update.  stdio is not robust about handling
 17134                      signals, which can cause an apparent I/O error.  */
 17135                   if (interrupt_input)
 17136                     unrequest_sigio ();
 17137                   STOP_POLLING;
 17138 
 17139                   pending |= update_frame (f, false, false);
 17140                   /* On some platforms (at least MS-Windows), the
 17141                      scroll_run_hook called from scrolling_window
 17142                      called from update_frame could set the frame's
 17143                      garbaged flag, in which case we need to redisplay
 17144                      the frame.  Don't do that on TTY frames, since we
 17145                      need to keep the garbaged flag in that case when
 17146                      the frame has been resized.  */
 17147                   if (FRAME_GARBAGED_P (f))
 17148                     {
 17149                       fset_redisplay (f);
 17150                       f->garbaged = false;
 17151                       goto retry_frame;
 17152                     }
 17153                   f->cursor_type_changed = false;
 17154                   f->updated_p = true;
 17155                   f->inhibit_clear_image_cache = false;
 17156                 }
 17157             }
 17158         }
 17159 
 17160       eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
 17161 
 17162       if (!pending)
 17163         {
 17164           /* Do the mark_window_display_accurate after all windows have
 17165              been redisplayed because this call resets flags in buffers
 17166              which are needed for proper redisplay.  */
 17167           FOR_EACH_FRAME (tail, frame)
 17168             {
 17169               struct frame *f = XFRAME (frame);
 17170               if (f->updated_p)
 17171                 {
 17172                   f->redisplay = false;
 17173                   f->garbaged = false;
 17174                   mark_window_display_accurate (f->root_window, true);
 17175                   if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
 17176                     FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
 17177                 }
 17178             }
 17179         }
 17180     }
 17181   else if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17182     {
 17183       sf->inhibit_clear_image_cache = true;
 17184       displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
 17185       /* Use list_of_error, not Qerror, so that
 17186          we catch only errors and don't run the debugger.  */
 17187       internal_condition_case_1 (redisplay_window_1, selected_window,
 17188                                  list_of_error,
 17189                                  redisplay_window_error);
 17190       if (update_miniwindow_p)
 17191         {
 17192           Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17193 
 17194           displayed_buffer = XBUFFER (XWINDOW (mini_window)->contents);
 17195           internal_condition_case_1 (redisplay_window_1, mini_window,
 17196                                      list_of_error,
 17197                                      redisplay_window_error);
 17198         }
 17199 
 17200       /* Compare desired and current matrices, perform output.  */
 17201 
 17202     update:
 17203       /* If fonts changed, display again.  Likewise if redisplay_window_1
 17204          above caused some change (e.g., a change in faces) that requires
 17205          considering the entire frame again.  */
 17206       if (sf->fonts_changed || sf->redisplay)
 17207         {
 17208           if (sf->redisplay)
 17209             {
 17210               /* Set this to force a more thorough redisplay.
 17211                  Otherwise, we might immediately loop back to the
 17212                  above "else-if" clause (since all the conditions that
 17213                  led here might still be true), and we will then
 17214                  infloop, because the selected-frame's redisplay flag
 17215                  is not (and cannot be) reset.  */
 17216               windows_or_buffers_changed = 50;
 17217             }
 17218           goto retry;
 17219         }
 17220 
 17221       /* Prevent freeing of realized faces, since desired matrices are
 17222          pending that reference the faces we computed and cached.  */
 17223       inhibit_free_realized_faces = true;
 17224 
 17225       /* Prevent various kinds of signals during display update.
 17226          stdio is not robust about handling signals,
 17227          which can cause an apparent I/O error.  */
 17228       if (interrupt_input)
 17229         unrequest_sigio ();
 17230       STOP_POLLING;
 17231 
 17232       if (FRAME_REDISPLAY_P (sf) && !FRAME_OBSCURED_P (sf))
 17233         {
 17234           if (hscroll_retries <= MAX_HSCROLL_RETRIES
 17235               && hscroll_windows (selected_window))
 17236             {
 17237               hscroll_retries++;
 17238               goto retry;
 17239             }
 17240 
 17241           XWINDOW (selected_window)->must_be_updated_p = true;
 17242           pending = update_frame (sf, false, false);
 17243           sf->cursor_type_changed = false;
 17244           sf->inhibit_clear_image_cache = false;
 17245         }
 17246 
 17247       /* We may have called echo_area_display at the top of this
 17248          function.  If the echo area is on another frame, that may
 17249          have put text on a frame other than the selected one, so the
 17250          above call to update_frame would not have caught it.  Catch
 17251          it here.  */
 17252       Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
 17253       struct frame *mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
 17254 
 17255       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
 17256         {
 17257           XWINDOW (mini_window)->must_be_updated_p = true;
 17258           pending |= update_frame (mini_frame, false, false);
 17259           mini_frame->cursor_type_changed = false;
 17260           if (!pending && hscroll_retries <= MAX_HSCROLL_RETRIES
 17261               && hscroll_windows (mini_window))
 17262             {
 17263               hscroll_retries++;
 17264               goto retry;
 17265             }
 17266         }
 17267     }
 17268 
 17269   /* If display was paused because of pending input, make sure we do a
 17270      thorough update the next time.  */
 17271   if (pending)
 17272     {
 17273       /* Prevent the optimization at the beginning of
 17274          redisplay_internal that tries a single-line update of the
 17275          line containing the cursor in the selected window.  */
 17276       CHARPOS (this_line_start_pos) = 0;
 17277 
 17278       /* Let the overlay arrow be updated the next time.  */
 17279       update_overlay_arrows (0);
 17280 
 17281       /* If we pause after scrolling, some rows in the current
 17282          matrices of some windows are not valid.  */
 17283       if (!WINDOW_FULL_WIDTH_P (w)
 17284           && !FRAME_WINDOW_P (XFRAME (w->frame)))
 17285         update_mode_lines = 36;
 17286     }
 17287   else
 17288     {
 17289       if (!consider_all_windows_p)
 17290         {
 17291           /* This has already been done above if
 17292              consider_all_windows_p is set.  */
 17293           if (XBUFFER (w->contents)->text->redisplay
 17294               && buffer_window_count (XBUFFER (w->contents)) > 1)
 17295             /* This can happen if b->text->redisplay was set during
 17296                jit-lock.  */
 17297             propagate_buffer_redisplay ();
 17298           mark_window_display_accurate_1 (w, true);
 17299 
 17300           /* Say overlay arrows are up to date.  */
 17301           update_overlay_arrows (1);
 17302 
 17303           if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
 17304             FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
 17305         }
 17306 
 17307       update_mode_lines = 0;
 17308       windows_or_buffers_changed = 0;
 17309     }
 17310 
 17311   /* Start SIGIO interrupts coming again.  Having them off during the
 17312      code above makes it less likely one will discard output, but not
 17313      impossible, since there might be stuff in the system buffer here.
 17314      But it is much hairier to try to do anything about that.  */
 17315   if (interrupt_input)
 17316     request_sigio ();
 17317   RESUME_POLLING;
 17318 
 17319   /* If a frame has become visible which was not before, redisplay
 17320      again, so that we display it.  Expose events for such a frame
 17321      (which it gets when becoming visible) don't call the parts of
 17322      redisplay constructing glyphs, so simply exposing a frame won't
 17323      display anything in this case.  So, we have to display these
 17324      frames here explicitly.  */
 17325   if (!pending)
 17326     {
 17327       int new_count = 0;
 17328 
 17329       FOR_EACH_FRAME (tail, frame)
 17330         {
 17331           if (FRAME_REDISPLAY_P (XFRAME (frame)))
 17332             new_count++;
 17333         }
 17334 
 17335       if (new_count != number_of_visible_frames)
 17336         windows_or_buffers_changed = 52;
 17337     }
 17338 
 17339   /* Change frame size now if a change is pending.  */
 17340   do_pending_window_change (true);
 17341 
 17342   /* If we just did a pending size change, or have additional
 17343      visible frames, or selected_window changed, redisplay again.  */
 17344   if ((windows_or_buffers_changed && !pending)
 17345       || (WINDOWP (selected_window)
 17346           && (w = XWINDOW (selected_window)) != sw))
 17347     goto retry;
 17348 
 17349   /* Clear the face and image caches.
 17350 
 17351      We used to do this only if consider_all_windows_p.  But the cache
 17352      needs to be cleared if a timer creates images in the current
 17353      buffer (e.g. the test case in Bug#6230).  */
 17354 
 17355   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
 17356     {
 17357       clear_face_cache (false);
 17358       clear_face_cache_count = 0;
 17359     }
 17360 
 17361 #ifdef HAVE_WINDOW_SYSTEM
 17362   if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
 17363     {
 17364       clear_image_caches (Qnil);
 17365       clear_image_cache_count = 0;
 17366     }
 17367 #endif /* HAVE_WINDOW_SYSTEM */
 17368 
 17369  end_of_redisplay:
 17370 #ifdef HAVE_NS
 17371   ns_set_doc_edited ();
 17372 #endif
 17373   if (interrupt_input && interrupts_deferred)
 17374     request_sigio ();
 17375 
 17376   /* We're done with this redisplay cycle, so reset the tick count in
 17377      preparation for the next redisplay cycle.  */
 17378   if (max_redisplay_ticks > 0)
 17379     update_redisplay_ticks (0, NULL);
 17380 
 17381   unbind_to (count, Qnil);
 17382   RESUME_POLLING;
 17383 }
 17384 
 17385 static void
 17386 unwind_redisplay_preserve_echo_area (void)
 17387 {
 17388   unblock_buffer_flips ();
 17389 }
 17390 
 17391 /* Redisplay, but leave alone any recent echo area message unless
 17392    another message has been requested in its place.
 17393 
 17394    This is useful in situations where you need to redisplay but no
 17395    user action has occurred, making it inappropriate for the message
 17396    area to be cleared.  See tracking_off and
 17397    wait_reading_process_output for examples of these situations.
 17398 
 17399    FROM_WHERE is an integer saying from where this function was
 17400    called.  This is useful for debugging.  */
 17401 
 17402 void
 17403 redisplay_preserve_echo_area (int from_where)
 17404 {
 17405   redisplay_trace ("redisplay_preserve_echo_area (%d)\n", from_where);
 17406 
 17407   block_input ();
 17408   specpdl_ref count = SPECPDL_INDEX ();
 17409   record_unwind_protect_void (unwind_redisplay_preserve_echo_area);
 17410   block_buffer_flips ();
 17411   unblock_input ();
 17412 
 17413   if (!NILP (echo_area_buffer[1]))
 17414     {
 17415       /* We have a previously displayed message, but no current
 17416          message.  Redisplay the previous message.  */
 17417       display_last_displayed_message_p = true;
 17418       redisplay_internal ();
 17419       display_last_displayed_message_p = false;
 17420     }
 17421   else
 17422     redisplay_internal ();
 17423 
 17424   flush_frame (SELECTED_FRAME ());
 17425   unbind_to (count, Qnil);
 17426 }
 17427 
 17428 
 17429 /* Function registered with record_unwind_protect in redisplay_internal.  */
 17430 
 17431 static void
 17432 unwind_redisplay (void)
 17433 {
 17434   redisplaying_p = false;
 17435   unblock_buffer_flips ();
 17436 }
 17437 
 17438 /* Function registered with record_unwind_protect before calling
 17439    start_display outside of redisplay_internal.  */
 17440 void
 17441 unwind_display_working_on_window (void)
 17442 {
 17443   display_working_on_window_p = false;
 17444 }
 17445 
 17446 /* Mark the display of leaf window W as accurate or inaccurate.
 17447    If ACCURATE_P, mark display of W as accurate.
 17448    If !ACCURATE_P, arrange for W to be redisplayed the next
 17449    time redisplay_internal is called.  */
 17450 
 17451 static void
 17452 mark_window_display_accurate_1 (struct window *w, bool accurate_p)
 17453 {
 17454   struct buffer *b = XBUFFER (w->contents);
 17455 
 17456   w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
 17457   w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
 17458   w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
 17459 
 17460   if (accurate_p)
 17461     {
 17462       b->clip_changed = false;
 17463       b->prevent_redisplay_optimizations_p = false;
 17464       eassert (buffer_window_count (b) > 0);
 17465       /* Resetting b->text->redisplay is problematic!
 17466          In order to make it safer to do it here, redisplay_internal must
 17467          have copied all b->text->redisplay to their respective windows.  */
 17468       b->text->redisplay = false;
 17469 
 17470       BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
 17471       BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
 17472       BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
 17473       BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
 17474 
 17475       w->current_matrix->buffer = b;
 17476       w->current_matrix->begv = BUF_BEGV (b);
 17477       w->current_matrix->zv = BUF_ZV (b);
 17478       w->current_matrix->header_line_p = window_wants_header_line (w);
 17479       w->current_matrix->tab_line_p = window_wants_tab_line (w);
 17480 
 17481       w->last_cursor_vpos = w->cursor.vpos;
 17482       w->last_cursor_off_p = w->cursor_off_p;
 17483 
 17484       if (w == XWINDOW (selected_window))
 17485         w->last_point = BUF_PT (b);
 17486       else
 17487         w->last_point = marker_position (w->pointm);
 17488 
 17489       w->window_end_valid = true;
 17490       w->update_mode_line = false;
 17491       w->preserve_vscroll_p = false;
 17492     }
 17493 
 17494   w->redisplay = !accurate_p;
 17495 }
 17496 
 17497 
 17498 /* Mark the display of windows in the window tree rooted at WINDOW as
 17499    accurate or inaccurate.  If ACCURATE_P, mark display of
 17500    windows as accurate.  If !ACCURATE_P, arrange for windows to
 17501    be redisplayed the next time redisplay_internal is called.  */
 17502 
 17503 void
 17504 mark_window_display_accurate (Lisp_Object window, bool accurate_p)
 17505 {
 17506   struct window *w;
 17507 
 17508   for (; !NILP (window); window = w->next)
 17509     {
 17510       w = XWINDOW (window);
 17511       if (WINDOWP (w->contents))
 17512         mark_window_display_accurate (w->contents, accurate_p);
 17513       else
 17514         mark_window_display_accurate_1 (w, accurate_p);
 17515     }
 17516 
 17517   if (accurate_p)
 17518     update_overlay_arrows (1);
 17519   else
 17520     /* Force a thorough redisplay the next time by setting
 17521        last_arrow_position and last_arrow_string to t, which is
 17522        unequal to any useful value of Voverlay_arrow_...  */
 17523     update_overlay_arrows (-1);
 17524 }
 17525 
 17526 
 17527 /* Return value in display table DP (Lisp_Char_Table *) for character
 17528    C.  Since a display table doesn't have any parent, we don't have to
 17529    follow parent.  Do not call this function directly but use the
 17530    macro DISP_CHAR_VECTOR.  */
 17531 
 17532 Lisp_Object
 17533 disp_char_vector (struct Lisp_Char_Table *dp, int c)
 17534 {
 17535   Lisp_Object val;
 17536 
 17537   if (ASCII_CHAR_P (c))
 17538     {
 17539       val = dp->ascii;
 17540       if (SUB_CHAR_TABLE_P (val))
 17541         val = XSUB_CHAR_TABLE (val)->contents[c];
 17542     }
 17543   else
 17544     {
 17545       Lisp_Object table;
 17546 
 17547       XSETCHAR_TABLE (table, dp);
 17548       val = char_table_ref (table, c);
 17549     }
 17550   if (NILP (val))
 17551     val = dp->defalt;
 17552   return val;
 17553 }
 17554 
 17555 static int buffer_flip_blocked_depth;
 17556 
 17557 static void
 17558 block_buffer_flips (void)
 17559 {
 17560   eassert (buffer_flip_blocked_depth >= 0);
 17561   buffer_flip_blocked_depth++;
 17562 }
 17563 
 17564 static void
 17565 unblock_buffer_flips (void)
 17566 {
 17567   eassert (buffer_flip_blocked_depth > 0);
 17568   if (--buffer_flip_blocked_depth == 0)
 17569     {
 17570       Lisp_Object tail, frame;
 17571       block_input ();
 17572       FOR_EACH_FRAME (tail, frame)
 17573         {
 17574           struct frame *f = XFRAME (frame);
 17575           if (FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook)
 17576             (*FRAME_TERMINAL (f)->buffer_flipping_unblocked_hook) (f);
 17577         }
 17578       unblock_input ();
 17579     }
 17580 }
 17581 
 17582 bool
 17583 buffer_flipping_blocked_p (void)
 17584 {
 17585   return buffer_flip_blocked_depth > 0;
 17586 }
 17587 
 17588 
 17589 /***********************************************************************
 17590                            Window Redisplay
 17591  ***********************************************************************/
 17592 
 17593 /* Redisplay all leaf windows in the window tree rooted at WINDOW.  */
 17594 
 17595 static void
 17596 redisplay_windows (Lisp_Object window)
 17597 {
 17598   while (!NILP (window))
 17599     {
 17600       struct window *w = XWINDOW (window);
 17601 
 17602       if (WINDOWP (w->contents))
 17603         redisplay_windows (w->contents);
 17604       else if (BUFFERP (w->contents))
 17605         {
 17606           displayed_buffer = XBUFFER (w->contents);
 17607           /* Use list_of_error, not Qerror, so that
 17608              we catch only errors and don't run the debugger.  */
 17609           internal_condition_case_1 (redisplay_window_0, window,
 17610                                      list_of_error,
 17611                                      redisplay_window_error);
 17612         }
 17613 
 17614       window = w->next;
 17615     }
 17616 }
 17617 
 17618 static Lisp_Object
 17619 redisplay_window_error (Lisp_Object error_data)
 17620 {
 17621   displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
 17622 
 17623   /* When in redisplay, the error is captured and not shown.  Arrange
 17624      for it to be shown later.  */
 17625   if (max_redisplay_ticks > 0
 17626       && CONSP (error_data)
 17627       && EQ (XCAR (error_data), Qerror)
 17628       && STRINGP (XCAR (XCDR (error_data))))
 17629     Vdelayed_warnings_list = Fcons (list2 (XCAR (error_data),
 17630                                            XCAR (XCDR (error_data))),
 17631                                     Vdelayed_warnings_list);
 17632   return Qnil;
 17633 }
 17634 
 17635 static Lisp_Object
 17636 redisplay_window_0 (Lisp_Object window)
 17637 {
 17638   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17639     redisplay_window (window, false);
 17640   return Qnil;
 17641 }
 17642 
 17643 static Lisp_Object
 17644 redisplay_window_1 (Lisp_Object window)
 17645 {
 17646   if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
 17647     redisplay_window (window, true);
 17648   return Qnil;
 17649 }
 17650 
 17651 
 17652 /***********************************************************************
 17653                       Aborting runaway redisplay
 17654  ***********************************************************************/
 17655 
 17656 /* Update the redisplay-tick count for window W, and signal an error
 17657    if the tick count is above some threshold, indicating that
 17658    redisplay of the window takes "too long".
 17659 
 17660    TICKS is the amount of ticks to add to the W's current count; zero
 17661    means to initialize the tick count to zero.
 17662 
 17663    W can be NULL if TICKS is zero: that means unconditionally
 17664    re-initialize the current tick count to zero.
 17665 
 17666    W can also be NULL if the caller doesn't know which window is being
 17667    processed by the display code.  In that case, if TICKS is non-zero,
 17668    we assume it's the last window that shows the current buffer.  */
 17669 void
 17670 update_redisplay_ticks (int ticks, struct window *w)
 17671 {
 17672   /* This keeps track of the window on which redisplay is working.  */
 17673   static struct window *cwindow;
 17674   static EMACS_INT window_ticks;
 17675 
 17676   /* We only initialize the count if this is a different window or
 17677      NULL.  Otherwise, this is a call from init_iterator for the same
 17678      window we tracked before, and we should keep the count.  */
 17679   if (!ticks && w != cwindow)
 17680     {
 17681       cwindow = w;
 17682       window_ticks = 0;
 17683     }
 17684   /* Some callers can be run in contexts unrelated to display code, so
 17685      don't abort them and don't update the tick count in those cases.  */
 17686   if ((!w && !redisplaying_p && !display_working_on_window_p)
 17687       /* We never disable redisplay of a mini-window, since that is
 17688          absolutely essential for communicating with Emacs.  */
 17689       || (w && MINI_WINDOW_P (w)))
 17690     return;
 17691 
 17692   if (ticks > 0)
 17693     window_ticks += ticks;
 17694   if (max_redisplay_ticks > 0 && window_ticks > max_redisplay_ticks)
 17695     {
 17696       /* In addition to a buffer, this could be a window (for non-leaf
 17697          windows, not expected here) or nil (for pseudo-windows like
 17698          the one used for the native tool bar).  */
 17699       Lisp_Object contents = w ? w->contents : Qnil;
 17700       char *bufname =
 17701         NILP (contents)
 17702         ? SSDATA (BVAR (current_buffer, name))
 17703         : (BUFFERP (contents)
 17704            ? SSDATA (BVAR (XBUFFER (contents), name))
 17705            : (char *) "<unknown>");
 17706 
 17707       windows_or_buffers_changed = 177;
 17708       /* scrolling_window depends too much on the glyph matrices being
 17709          correct, and we cannot guarantee that if we abort the
 17710          redisplay of this window.  */
 17711       if (w && w->desired_matrix)
 17712         w->desired_matrix->no_scrolling_p = true;
 17713       error ("Window showing buffer %s takes too long to redisplay", bufname);
 17714     }
 17715 }
 17716 
 17717 
 17718 
 17719 /* Set cursor position of W.  PT is assumed to be displayed in ROW.
 17720    DELTA and DELTA_BYTES are the numbers of characters and bytes by
 17721    which positions recorded in ROW differ from current buffer
 17722    positions.
 17723 
 17724    Return true iff cursor is on this row.  */
 17725 
 17726 static bool
 17727 set_cursor_from_row (struct window *w, struct glyph_row *row,
 17728                      struct glyph_matrix *matrix,
 17729                      ptrdiff_t delta, ptrdiff_t delta_bytes,
 17730                      int dy, int dvpos)
 17731 {
 17732   struct glyph *glyph = row->glyphs[TEXT_AREA];
 17733   struct glyph *end = glyph + row->used[TEXT_AREA];
 17734   struct glyph *cursor = NULL;
 17735   /* The last known character position in row.  */
 17736   ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
 17737   int x = row->x;
 17738   ptrdiff_t pt_old = PT - delta;
 17739   ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
 17740   ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 17741   struct glyph *glyph_before = glyph - 1, *glyph_after = end;
 17742   /* A glyph beyond the edge of TEXT_AREA which we should never
 17743      touch.  */
 17744   struct glyph *glyphs_end = end;
 17745   /* True means we've found a match for cursor position, but that
 17746      glyph has the avoid_cursor_p flag set.  */
 17747   bool match_with_avoid_cursor = false;
 17748   /* True means we've seen at least one glyph that came from a
 17749      display string.  */
 17750   bool string_seen = false;
 17751   /* Largest and smallest buffer positions seen so far during scan of
 17752      glyph row.  */
 17753   ptrdiff_t bpos_max = pos_before;
 17754   ptrdiff_t bpos_min = pos_after;
 17755   /* Last buffer position covered by an overlay string with an integer
 17756      `cursor' property.  */
 17757   ptrdiff_t bpos_covered = 0;
 17758   /* True means the display string on which to display the cursor
 17759      comes from a text property, not from an overlay.  */
 17760   bool string_from_text_prop = false;
 17761 
 17762   /* Don't even try doing anything if called for a mode-line or
 17763      header-line or tab-line row, since the rest of the code isn't
 17764      prepared to deal with such calamities.  */
 17765   eassert (!row->mode_line_p);
 17766   if (row->mode_line_p)
 17767     return false;
 17768 
 17769   /* Skip over glyphs not having an object at the start and the end of
 17770      the row.  These are special glyphs like truncation marks on
 17771      terminal frames.  */
 17772   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 17773     {
 17774       if (!row->reversed_p)
 17775         {
 17776           while (glyph < end
 17777                  && NILP (glyph->object)
 17778                  && glyph->charpos < 0)
 17779             {
 17780               x += glyph->pixel_width;
 17781               ++glyph;
 17782             }
 17783           while (end > glyph
 17784                  && NILP ((end - 1)->object)
 17785                  /* CHARPOS is zero for blanks and stretch glyphs
 17786                     inserted by extend_face_to_end_of_line.  */
 17787                  && (end - 1)->charpos <= 0)
 17788             --end;
 17789           glyph_before = glyph - 1;
 17790           glyph_after = end;
 17791         }
 17792       else
 17793         {
 17794           struct glyph *g;
 17795 
 17796           /* If the glyph row is reversed, we need to process it from back
 17797              to front, so swap the edge pointers.  */
 17798           glyphs_end = end = glyph - 1;
 17799           glyph += row->used[TEXT_AREA] - 1;
 17800 
 17801           while (glyph > end + 1
 17802                  && NILP (glyph->object)
 17803                  && glyph->charpos < 0)
 17804             --glyph;
 17805           if (NILP (glyph->object) && glyph->charpos < 0)
 17806             --glyph;
 17807           /* By default, in reversed rows we put the cursor on the
 17808              rightmost (first in the reading order) glyph.  */
 17809           for (x = 0, g = end + 1; g < glyph; g++)
 17810             x += g->pixel_width;
 17811           while (end < glyph
 17812                  && NILP ((end + 1)->object)
 17813                  && (end + 1)->charpos <= 0)
 17814             ++end;
 17815           glyph_before = glyph + 1;
 17816           glyph_after = end;
 17817         }
 17818     }
 17819   else if (row->reversed_p)
 17820     {
 17821       /* In R2L rows that don't display text, put the cursor on the
 17822          rightmost glyph.  Case in point: an empty last line that is
 17823          part of an R2L paragraph.  */
 17824       cursor = end - 1;
 17825       /* Avoid placing the cursor on the last glyph of the row, where
 17826          on terminal frames we hold the vertical border between
 17827          adjacent windows.  */
 17828       if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
 17829           && !WINDOW_RIGHTMOST_P (w)
 17830           && cursor == row->glyphs[LAST_AREA] - 1)
 17831         cursor--;
 17832       x = -1;   /* will be computed below, at label compute_x */
 17833     }
 17834 
 17835   /* Step 1: Try to find the glyph whose character position
 17836      corresponds to point.  If that's not possible, find 2 glyphs
 17837      whose character positions are the closest to point, one before
 17838      point, the other after it.  */
 17839   if (!row->reversed_p)
 17840     while (/* not marched to end of glyph row */
 17841            glyph < end
 17842            /* glyph was not inserted by redisplay for internal purposes */
 17843            && !NILP (glyph->object))
 17844       {
 17845         if (BUFFERP (glyph->object))
 17846           {
 17847             ptrdiff_t dpos = glyph->charpos - pt_old;
 17848 
 17849             if (glyph->charpos > bpos_max)
 17850               bpos_max = glyph->charpos;
 17851             if (glyph->charpos < bpos_min)
 17852               bpos_min = glyph->charpos;
 17853             if (!glyph->avoid_cursor_p)
 17854               {
 17855                 /* If we hit point, we've found the glyph on which to
 17856                    display the cursor.  */
 17857                 if (dpos == 0)
 17858                   {
 17859                     match_with_avoid_cursor = false;
 17860                     break;
 17861                   }
 17862                 /* See if we've found a better approximation to
 17863                    POS_BEFORE or to POS_AFTER.  */
 17864                 if (0 > dpos && dpos > pos_before - pt_old)
 17865                   {
 17866                     pos_before = glyph->charpos;
 17867                     glyph_before = glyph;
 17868                   }
 17869                 else if (0 < dpos && dpos < pos_after - pt_old)
 17870                   {
 17871                     pos_after = glyph->charpos;
 17872                     glyph_after = glyph;
 17873                   }
 17874               }
 17875             else if (dpos == 0)
 17876               match_with_avoid_cursor = true;
 17877           }
 17878         else if (STRINGP (glyph->object))
 17879           {
 17880             Lisp_Object chprop;
 17881             ptrdiff_t glyph_pos = glyph->charpos;
 17882 
 17883             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17884                                          glyph->object);
 17885             if (!NILP (chprop))
 17886               {
 17887                 /* If the string came from a `display' text property,
 17888                    look up the buffer position of that property and
 17889                    use that position to update bpos_max, as if we
 17890                    actually saw such a position in one of the row's
 17891                    glyphs.  This helps with supporting integer values
 17892                    of `cursor' property on the display string in
 17893                    situations where most or all of the row's buffer
 17894                    text is completely covered by display properties,
 17895                    so that no glyph with valid buffer positions is
 17896                    ever seen in the row.  */
 17897                 ptrdiff_t prop_pos =
 17898                   string_buffer_position_lim (glyph->object, pos_before,
 17899                                               pos_after, false);
 17900 
 17901                 if (prop_pos >= pos_before)
 17902                   bpos_max = prop_pos;
 17903               }
 17904             if (FIXNUMP (chprop))
 17905               {
 17906                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17907                 /* If the `cursor' property covers buffer positions up
 17908                    to and including point, we should display cursor on
 17909                    this glyph.  Note that, if a `cursor' property on one
 17910                    of the string's characters has an integer value, we
 17911                    will break out of the loop below _before_ we get to
 17912                    the position match above.  IOW, integer values of
 17913                    the `cursor' property override the "exact match for
 17914                    point" strategy of positioning the cursor.  */
 17915                 /* Implementation note: bpos_max == pt_old when, e.g.,
 17916                    we are in an empty line, where bpos_max is set to
 17917                    MATRIX_ROW_START_CHARPOS, see above.  */
 17918                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17919                   {
 17920                     cursor = glyph;
 17921                     break;
 17922                   }
 17923               }
 17924 
 17925             string_seen = true;
 17926           }
 17927         x += glyph->pixel_width;
 17928         ++glyph;
 17929       }
 17930   else if (glyph > end) /* row is reversed */
 17931     while (!NILP (glyph->object))
 17932       {
 17933         if (BUFFERP (glyph->object))
 17934           {
 17935             ptrdiff_t dpos = glyph->charpos - pt_old;
 17936 
 17937             if (glyph->charpos > bpos_max)
 17938               bpos_max = glyph->charpos;
 17939             if (glyph->charpos < bpos_min)
 17940               bpos_min = glyph->charpos;
 17941             if (!glyph->avoid_cursor_p)
 17942               {
 17943                 if (dpos == 0)
 17944                   {
 17945                     match_with_avoid_cursor = false;
 17946                     break;
 17947                   }
 17948                 if (0 > dpos && dpos > pos_before - pt_old)
 17949                   {
 17950                     pos_before = glyph->charpos;
 17951                     glyph_before = glyph;
 17952                   }
 17953                 else if (0 < dpos && dpos < pos_after - pt_old)
 17954                   {
 17955                     pos_after = glyph->charpos;
 17956                     glyph_after = glyph;
 17957                   }
 17958               }
 17959             else if (dpos == 0)
 17960               match_with_avoid_cursor = true;
 17961           }
 17962         else if (STRINGP (glyph->object))
 17963           {
 17964             Lisp_Object chprop;
 17965             ptrdiff_t glyph_pos = glyph->charpos;
 17966 
 17967             chprop = Fget_char_property (make_fixnum (glyph_pos), Qcursor,
 17968                                          glyph->object);
 17969             if (!NILP (chprop))
 17970               {
 17971                 ptrdiff_t prop_pos =
 17972                   string_buffer_position_lim (glyph->object, pos_before,
 17973                                               pos_after, false);
 17974 
 17975                 if (prop_pos >= pos_before)
 17976                   bpos_max = prop_pos;
 17977               }
 17978             if (FIXNUMP (chprop))
 17979               {
 17980                 bpos_covered = bpos_max + XFIXNUM (chprop);
 17981                 /* If the `cursor' property covers buffer positions up
 17982                    to and including point, we should display cursor on
 17983                    this glyph.  */
 17984                 if (bpos_max <= pt_old && bpos_covered >= pt_old)
 17985                   {
 17986                     cursor = glyph;
 17987                     break;
 17988                   }
 17989               }
 17990             string_seen = true;
 17991           }
 17992         --glyph;
 17993         if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
 17994           {
 17995             x--;                /* can't use any pixel_width */
 17996             break;
 17997           }
 17998         x -= glyph->pixel_width;
 17999     }
 18000 
 18001   /* Step 2: If we didn't find an exact match for point, we need to
 18002      look for a proper place to put the cursor among glyphs between
 18003      GLYPH_BEFORE and GLYPH_AFTER.  */
 18004   if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18005         && BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18006       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
 18007     {
 18008       /* An empty line has a single glyph whose OBJECT is nil and
 18009          whose CHARPOS is the position of a newline on that line.
 18010          Note that on a TTY, there are more glyphs after that, which
 18011          were produced by extend_face_to_end_of_line, but their
 18012          CHARPOS is zero or negative.  */
 18013       bool empty_line_p =
 18014         ((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
 18015          && NILP (glyph->object) && glyph->charpos > 0
 18016          /* On a TTY, continued and truncated rows also have a glyph at
 18017             their end whose OBJECT is nil and whose CHARPOS is
 18018             positive (the continuation and truncation glyphs), but such
 18019             rows are obviously not "empty".  */
 18020          && !(row->continued_p || row->truncated_on_right_p));
 18021 
 18022       if (row->ends_in_ellipsis_p && pos_after == last_pos)
 18023         {
 18024           ptrdiff_t ellipsis_pos;
 18025 
 18026           /* Scan back over the ellipsis glyphs.  */
 18027           if (!row->reversed_p)
 18028             {
 18029               ellipsis_pos = (glyph - 1)->charpos;
 18030               while (glyph > row->glyphs[TEXT_AREA]
 18031                      && (glyph - 1)->charpos == ellipsis_pos)
 18032                 glyph--, x -= glyph->pixel_width;
 18033               /* That loop always goes one position too far, including
 18034                  the glyph before the ellipsis.  So scan forward over
 18035                  that one.  */
 18036               x += glyph->pixel_width;
 18037               glyph++;
 18038             }
 18039           else  /* row is reversed */
 18040             {
 18041               ellipsis_pos = (glyph + 1)->charpos;
 18042               while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18043                      && (glyph + 1)->charpos == ellipsis_pos)
 18044                 glyph++, x += glyph->pixel_width;
 18045               x -= glyph->pixel_width;
 18046               glyph--;
 18047             }
 18048         }
 18049       else if (match_with_avoid_cursor)
 18050         {
 18051           cursor = glyph_after;
 18052           x = -1;
 18053         }
 18054       else if (string_seen)
 18055         {
 18056           int incr = row->reversed_p ? -1 : +1;
 18057 
 18058           /* Need to find the glyph that came out of a string which is
 18059              present at point.  That glyph is somewhere between
 18060              GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
 18061              positioned between POS_BEFORE and POS_AFTER in the
 18062              buffer.  */
 18063           struct glyph *start, *stop;
 18064           ptrdiff_t pos = pos_before;
 18065 
 18066           x = -1;
 18067 
 18068           /* If the row ends in a newline from a display string,
 18069              reordering could have moved the glyphs belonging to the
 18070              string out of the [GLYPH_BEFORE..GLYPH_AFTER] range.  So
 18071              in this case we extend the search to the last glyph in
 18072              the row that was not inserted by redisplay.  */
 18073           if (row->ends_in_newline_from_string_p)
 18074             {
 18075               glyph_after = end;
 18076               pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
 18077             }
 18078 
 18079           /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
 18080              correspond to POS_BEFORE and POS_AFTER, respectively.  We
 18081              need START and STOP in the order that corresponds to the
 18082              row's direction as given by its reversed_p flag.  If the
 18083              directionality of characters between POS_BEFORE and
 18084              POS_AFTER is the opposite of the row's base direction,
 18085              these characters will have been reordered for display,
 18086              and we need to reverse START and STOP.  */
 18087           if (!row->reversed_p)
 18088             {
 18089               start = min (glyph_before, glyph_after);
 18090               stop = max (glyph_before, glyph_after);
 18091             }
 18092           else
 18093             {
 18094               start = max (glyph_before, glyph_after);
 18095               stop = min (glyph_before, glyph_after);
 18096             }
 18097           for (glyph = start + incr;
 18098                row->reversed_p ? glyph > stop : glyph < stop; )
 18099             {
 18100 
 18101               /* Any glyphs that come from the buffer are here because
 18102                  of bidi reordering.  Skip them, and only pay
 18103                  attention to glyphs that came from some string.  */
 18104               if (STRINGP (glyph->object))
 18105                 {
 18106                   Lisp_Object str;
 18107                   ptrdiff_t tem;
 18108                   /* If the display property covers the newline, we
 18109                      need to search for it one position farther.  */
 18110                   ptrdiff_t lim = pos_after
 18111                     + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
 18112 
 18113                   string_from_text_prop = false;
 18114                   str = glyph->object;
 18115                   tem = string_buffer_position_lim (str, pos, lim, false);
 18116                   if (tem == 0  /* from overlay */
 18117                       || pos <= tem)
 18118                     {
 18119                       /* If the string from which this glyph came is
 18120                          found in the buffer at point, or at position
 18121                          that is closer to point than pos_after, then
 18122                          we've found the glyph we've been looking for.
 18123                          If it comes from an overlay (tem == 0), and
 18124                          it has the `cursor' property on one of its
 18125                          glyphs, record that glyph as a candidate for
 18126                          displaying the cursor.  (As in the
 18127                          unidirectional version, we will display the
 18128                          cursor on the last candidate we find.)  */
 18129                       if (tem == 0
 18130                           || tem == pt_old
 18131                           || (tem - pt_old > 0 && tem < pos_after))
 18132                         {
 18133                           /* The glyphs from this string could have
 18134                              been reordered.  Find the one with the
 18135                              smallest string position.  Or there could
 18136                              be a character in the string with the
 18137                              `cursor' property, which means display
 18138                              cursor on that character's glyph.  */
 18139                           ptrdiff_t strpos = glyph->charpos;
 18140 
 18141                           if (tem)
 18142                             {
 18143                               cursor = glyph;
 18144                               string_from_text_prop = true;
 18145                             }
 18146                           for ( ;
 18147                                (row->reversed_p ? glyph > stop : glyph < stop)
 18148                                  && EQ (glyph->object, str);
 18149                                glyph += incr)
 18150                             {
 18151                               Lisp_Object cprop;
 18152                               ptrdiff_t gpos = glyph->charpos;
 18153 
 18154                               cprop = Fget_char_property (make_fixnum (gpos),
 18155                                                           Qcursor,
 18156                                                           glyph->object);
 18157                               if (!NILP (cprop))
 18158                                 {
 18159                                   cursor = glyph;
 18160                                   break;
 18161                                 }
 18162                               if (tem && glyph->charpos < strpos)
 18163                                 {
 18164                                   strpos = glyph->charpos;
 18165                                   cursor = glyph;
 18166                                 }
 18167                             }
 18168 
 18169                           if (tem == pt_old
 18170                               || (tem - pt_old > 0 && tem < pos_after))
 18171                             goto compute_x;
 18172                         }
 18173                       if (tem)
 18174                         pos = tem + 1; /* don't find previous instances */
 18175                     }
 18176                   /* This string is not what we want; skip all of the
 18177                      glyphs that came from it.  */
 18178                   while ((row->reversed_p ? glyph > stop : glyph < stop)
 18179                          && EQ (glyph->object, str))
 18180                     glyph += incr;
 18181                 }
 18182               else
 18183                 glyph += incr;
 18184             }
 18185 
 18186           /* If we reached the end of the line, and END was from a string,
 18187              the cursor is not on this line.  */
 18188           if (cursor == NULL
 18189               && (row->reversed_p ? glyph <= end : glyph >= end)
 18190               && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
 18191               && STRINGP (end->object)
 18192               && row->continued_p)
 18193             return false;
 18194         }
 18195       /* A truncated row may not include PT among its character positions.
 18196          Setting the cursor inside the scroll margin will trigger
 18197          recalculation of hscroll in hscroll_window_tree.  But if a
 18198          display string covers point, defer to the string-handling
 18199          code below to figure this out.  */
 18200       else if (row->truncated_on_left_p && pt_old < bpos_min)
 18201         {
 18202           cursor = glyph_before;
 18203           x = -1;
 18204         }
 18205       else if ((row->truncated_on_right_p && pt_old > bpos_max)
 18206                /* Zero-width characters produce no glyphs.  */
 18207                || (!empty_line_p
 18208                    && (row->reversed_p
 18209                        ? glyph_after > glyphs_end
 18210                        : glyph_after < glyphs_end)))
 18211         {
 18212           cursor = glyph_after;
 18213           x = -1;
 18214         }
 18215     }
 18216 
 18217  compute_x:
 18218   if (cursor != NULL)
 18219     glyph = cursor;
 18220   else if (glyph == glyphs_end
 18221            && pos_before == pos_after
 18222            && STRINGP ((row->reversed_p
 18223                         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18224                         : row->glyphs[TEXT_AREA])->object))
 18225     {
 18226       /* If all the glyphs of this row came from strings, put the
 18227          cursor on the first glyph of the row.  This avoids having the
 18228          cursor outside of the text area in this very rare and hard
 18229          use case.  */
 18230       glyph =
 18231         row->reversed_p
 18232         ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
 18233         : row->glyphs[TEXT_AREA];
 18234     }
 18235   if (x < 0)
 18236     {
 18237       struct glyph *g;
 18238 
 18239       /* Need to compute x that corresponds to GLYPH.  */
 18240       for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
 18241         {
 18242           if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
 18243             emacs_abort ();
 18244           x += g->pixel_width;
 18245         }
 18246     }
 18247 
 18248   /* ROW could be part of a continued line, which, under bidi
 18249      reordering, might have other rows whose start and end charpos
 18250      occlude point.  Only set w->cursor if we found a better
 18251      approximation to the cursor position than we have from previously
 18252      examined candidate rows belonging to the same continued line.  */
 18253   if (/* We already have a candidate row.  */
 18254       w->cursor.vpos >= 0
 18255       /* That candidate is not the row we are processing.  */
 18256       && MATRIX_ROW (matrix, w->cursor.vpos) != row
 18257       /* Make sure cursor.vpos specifies a row whose start and end
 18258          charpos occlude point, and it is valid candidate for being a
 18259          cursor-row.  This is because some callers of this function
 18260          leave cursor.vpos at the row where the cursor was displayed
 18261          during the last redisplay cycle.  */
 18262       && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
 18263       && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18264       && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
 18265     {
 18266       struct glyph *g1
 18267         = MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
 18268 
 18269       /* Don't consider glyphs that are outside TEXT_AREA.  */
 18270       if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
 18271         return false;
 18272       /* Keep the candidate whose buffer position is the closest to
 18273          point or has the `cursor' property.  */
 18274       if (/* Previous candidate is a glyph in TEXT_AREA of that row.  */
 18275           w->cursor.hpos >= 0
 18276           && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
 18277           && ((BUFFERP (g1->object)
 18278                && (g1->charpos == pt_old /* An exact match always wins.  */
 18279                    || (BUFFERP (glyph->object)
 18280                        && eabs (g1->charpos - pt_old)
 18281                        < eabs (glyph->charpos - pt_old))))
 18282               /* Previous candidate is a glyph from a string that has
 18283                  a non-nil `cursor' property.  */
 18284               || (STRINGP (g1->object)
 18285                   && (!NILP (Fget_char_property (make_fixnum (g1->charpos),
 18286                                                 Qcursor, g1->object))
 18287                       /* Previous candidate is from the same display
 18288                          string as this one, and the display string
 18289                          came from a text property.  */
 18290                       || (EQ (g1->object, glyph->object)
 18291                           && string_from_text_prop)
 18292                       /* this candidate is from newline and its
 18293                          position is not an exact match */
 18294                       || (NILP (glyph->object)
 18295                           && glyph->charpos != pt_old)))))
 18296         return false;
 18297       /* If this candidate gives an exact match, use that.  */
 18298       if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
 18299             /* If this candidate is a glyph created for the
 18300                terminating newline of a line, and point is on that
 18301                newline, it wins because it's an exact match.  */
 18302             || (!row->continued_p
 18303                 && NILP (glyph->object)
 18304                 && glyph->charpos == 0
 18305                 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
 18306           /* Otherwise, keep the candidate that comes from a row
 18307              spanning less buffer positions.  This may win when one or
 18308              both candidate positions are on glyphs that came from
 18309              display strings, for which we cannot compare buffer
 18310              positions.  */
 18311           && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18312              - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
 18313              < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
 18314         return false;
 18315     }
 18316   w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
 18317   w->cursor.x = x;
 18318   w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
 18319   w->cursor.y = row->y + dy;
 18320 
 18321   if (w == XWINDOW (selected_window))
 18322     {
 18323       if (!row->continued_p
 18324           && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 18325           && row->x == 0)
 18326         {
 18327           this_line_buffer = XBUFFER (w->contents);
 18328 
 18329           CHARPOS (this_line_start_pos)
 18330             = MATRIX_ROW_START_CHARPOS (row) + delta;
 18331           BYTEPOS (this_line_start_pos)
 18332             = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
 18333 
 18334           CHARPOS (this_line_end_pos)
 18335             = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
 18336           BYTEPOS (this_line_end_pos)
 18337             = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
 18338 
 18339           this_line_y = w->cursor.y;
 18340           this_line_pixel_height = row->height;
 18341           this_line_vpos = w->cursor.vpos;
 18342           this_line_start_x = row->x;
 18343         }
 18344       else
 18345         CHARPOS (this_line_start_pos) = 0;
 18346     }
 18347 
 18348   return true;
 18349 }
 18350 
 18351 
 18352 /* Run window scroll functions, if any, for WINDOW with new window
 18353    start STARTP.  Sets the window start of WINDOW to that position.
 18354 
 18355    We assume that the window's buffer is really current.  */
 18356 
 18357 static struct text_pos
 18358 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
 18359 {
 18360   struct window *w = XWINDOW (window);
 18361   SET_MARKER_FROM_TEXT_POS (w->start, startp);
 18362 
 18363   eassert (current_buffer == XBUFFER (w->contents));
 18364 
 18365   if (!NILP (Vwindow_scroll_functions))
 18366     {
 18367       specpdl_ref count = SPECPDL_INDEX ();
 18368       specbind (Qinhibit_quit, Qt);
 18369       safe_run_hooks_2
 18370         (Qwindow_scroll_functions, window, make_fixnum (CHARPOS (startp)));
 18371       unbind_to (count, Qnil);
 18372       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18373       /* In case the hook functions switch buffers.  */
 18374       set_buffer_internal (XBUFFER (w->contents));
 18375     }
 18376 
 18377   return startp;
 18378 }
 18379 
 18380 
 18381 /* Make sure the line containing the cursor is fully visible.
 18382    A value of true means there is nothing to be done.
 18383    (Either the line is fully visible, or it cannot be made so,
 18384    or we cannot tell.)
 18385 
 18386    If FORCE_P, return false even if partial visible cursor row
 18387    is higher than window.
 18388 
 18389    If CURRENT_MATRIX_P, use the information from the
 18390    window's current glyph matrix; otherwise use the desired glyph
 18391    matrix.
 18392 
 18393    If JUST_TEST_USER_PREFERENCE_P, just test what the value of
 18394    make-cursor-row-fully-visible requires, don't test the actual
 18395    cursor position.  The assumption is that in that case the caller
 18396    performs the necessary testing of the cursor position.
 18397 
 18398    A value of false means the caller should do scrolling
 18399    as if point had gone off the screen.  */
 18400 
 18401 static bool
 18402 cursor_row_fully_visible_p (struct window *w, bool force_p,
 18403                             bool current_matrix_p,
 18404                             bool just_test_user_preference_p)
 18405 {
 18406   struct glyph_matrix *matrix;
 18407   struct glyph_row *row;
 18408   int window_height;
 18409   Lisp_Object mclfv_p =
 18410     buffer_local_value (Qmake_cursor_line_fully_visible, w->contents);
 18411 
 18412   /* If no local binding, use the global value.  */
 18413   if (BASE_EQ (mclfv_p, Qunbound))
 18414     mclfv_p = Vmake_cursor_line_fully_visible;
 18415   /* Follow mode sets the variable to a Lisp function in buffers that
 18416      are under Follow mode.  */
 18417   if (FUNCTIONP (mclfv_p))
 18418     {
 18419       Lisp_Object window;
 18420       XSETWINDOW (window, w);
 18421       /* Implementation note: if the function we call here signals an
 18422          error, we will NOT scroll when the cursor is partially-visible.  */
 18423       Lisp_Object val = safe_call1 (mclfv_p, window);
 18424       if (NILP (val))
 18425         return true;
 18426       else if (just_test_user_preference_p)
 18427         return false;
 18428     }
 18429   else if (NILP (mclfv_p))
 18430     return true;
 18431   else if (just_test_user_preference_p)
 18432     return false;
 18433 
 18434   /* It's not always possible to find the cursor, e.g, when a window
 18435      is full of overlay strings.  Don't do anything in that case.  */
 18436   if (w->cursor.vpos < 0)
 18437     return true;
 18438 
 18439   matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
 18440   row = MATRIX_ROW (matrix, w->cursor.vpos);
 18441 
 18442   /* If the cursor row is not partially visible, there's nothing to do.  */
 18443   if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
 18444     return true;
 18445 
 18446   /* If the row the cursor is in is taller than the window's height,
 18447      it's not clear what to do, so do nothing.  */
 18448   window_height = window_box_height (w);
 18449   if (row->height >= window_height)
 18450     {
 18451       if (!force_p || MINI_WINDOW_P (w)
 18452           || w->vscroll || w->cursor.vpos == 0)
 18453         return true;
 18454     }
 18455   return false;
 18456 }
 18457 
 18458 
 18459 /* Try scrolling PT into view in window WINDOW.  JUST_THIS_ONE_P
 18460    means only WINDOW is redisplayed in redisplay_internal.
 18461    TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
 18462    in redisplay_window to bring a partially visible line into view in
 18463    the case that only the cursor has moved.
 18464 
 18465    LAST_LINE_MISFIT should be true if we're scrolling because the
 18466    last screen line's vertical height extends past the end of the screen.
 18467 
 18468    Value is
 18469 
 18470    1    if scrolling succeeded
 18471 
 18472    0    if scrolling didn't find point.
 18473 
 18474    -1   if new fonts have been loaded so that we must interrupt
 18475    redisplay, adjust glyph matrices, and try again.  */
 18476 
 18477 enum
 18478 {
 18479   SCROLLING_SUCCESS = 1,
 18480   SCROLLING_FAILED = 0,
 18481   SCROLLING_NEED_LARGER_MATRICES = -1
 18482 };
 18483 
 18484 /* If scroll-conservatively is more than this, never recenter.
 18485 
 18486    If you change this, don't forget to update the doc string of
 18487    `scroll-conservatively' and the Emacs manual.  */
 18488 #define SCROLL_LIMIT 100
 18489 
 18490 static int
 18491 try_scrolling (Lisp_Object window, bool just_this_one_p,
 18492                intmax_t arg_scroll_conservatively, intmax_t scroll_step,
 18493                bool temp_scroll_step, bool last_line_misfit)
 18494 {
 18495   struct window *w = XWINDOW (window);
 18496   struct text_pos pos, startp;
 18497   struct it it;
 18498   int this_scroll_margin, scroll_max, rc, height;
 18499   int dy = 0, amount_to_scroll = 0;
 18500   bool scroll_down_p = false;
 18501   int extra_scroll_margin_lines = last_line_misfit;
 18502   Lisp_Object aggressive;
 18503   /* We will never try scrolling more than this number of lines.  */
 18504   int scroll_limit = SCROLL_LIMIT;
 18505   int frame_line_height = default_line_pixel_height (w);
 18506 
 18507 #ifdef GLYPH_DEBUG
 18508   debug_method_add (w, "try_scrolling");
 18509 #endif
 18510 
 18511   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 18512 
 18513   this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 18514 
 18515   /* Force arg_scroll_conservatively to have a reasonable value, to
 18516      avoid scrolling too far away with slow move_it_* functions.  Note
 18517      that the user can supply scroll-conservatively equal to
 18518      `most-positive-fixnum', which can be larger than INT_MAX.  */
 18519   if (arg_scroll_conservatively > scroll_limit)
 18520     {
 18521       arg_scroll_conservatively = scroll_limit + 1;
 18522       scroll_max = scroll_limit * frame_line_height;
 18523     }
 18524   else if (0 < scroll_step || 0 < arg_scroll_conservatively || temp_scroll_step)
 18525     /* Compute how much we should try to scroll maximally to bring
 18526        point into view.  */
 18527     {
 18528       intmax_t scroll_lines_max
 18529         = max (scroll_step, max (arg_scroll_conservatively, temp_scroll_step));
 18530       int scroll_lines = clip_to_bounds (0, scroll_lines_max, 1000000);
 18531       scroll_max = scroll_lines * frame_line_height;
 18532     }
 18533   else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
 18534            || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
 18535     /* We're trying to scroll because of aggressive scrolling but no
 18536        scroll_step is set.  Choose an arbitrary one.  */
 18537     scroll_max = 10 * frame_line_height;
 18538   else
 18539     scroll_max = 0;
 18540 
 18541  too_near_end:
 18542 
 18543   /* Decide whether to scroll down.  */
 18544   if (PT > CHARPOS (startp))
 18545     {
 18546       int scroll_margin_y;
 18547 
 18548       /* Compute the pixel ypos of the scroll margin, then move IT to
 18549          either that ypos or PT, whichever comes first.  */
 18550       start_display (&it, w, startp);
 18551       scroll_margin_y = it.last_visible_y - partial_line_height (&it)
 18552         - this_scroll_margin
 18553         - frame_line_height * extra_scroll_margin_lines;
 18554       move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
 18555                   (MOVE_TO_POS | MOVE_TO_Y));
 18556 
 18557       if (PT > CHARPOS (it.current.pos))
 18558         {
 18559           int y0 = line_bottom_y (&it);
 18560           /* Compute how many pixels below window bottom to stop searching
 18561              for PT.  This avoids costly search for PT that is far away if
 18562              the user limited scrolling by a small number of lines, but
 18563              always finds PT if scroll_conservatively is set to a large
 18564              number, such as most-positive-fixnum.  */
 18565           int slack = max (scroll_max, 10 * frame_line_height);
 18566           int y_to_move = it.last_visible_y + slack;
 18567 
 18568           /* Compute the distance from the scroll margin to PT or to
 18569              the scroll limit, whichever comes first.  This should
 18570              include the height of the cursor line, to make that line
 18571              fully visible.  */
 18572           move_it_to (&it, PT, -1, y_to_move,
 18573                       -1, MOVE_TO_POS | MOVE_TO_Y);
 18574           dy = line_bottom_y (&it) - y0;
 18575 
 18576           if (dy > scroll_max)
 18577             return SCROLLING_FAILED;
 18578 
 18579           if (dy > 0)
 18580             scroll_down_p = true;
 18581         }
 18582       else if (PT == IT_CHARPOS (it)
 18583                && IT_CHARPOS (it) < ZV
 18584                && it.method == GET_FROM_STRING
 18585                && arg_scroll_conservatively > scroll_limit
 18586                && it.current_x == 0)
 18587         {
 18588           enum move_it_result skip;
 18589           int y1 = it.current_y;
 18590           int vpos;
 18591 
 18592           /* A before-string that includes newlines and is displayed
 18593              on the last visible screen line could fail us under
 18594              scroll-conservatively > 100, because we will be unable to
 18595              position the cursor on that last visible line.  Try to
 18596              recover by finding the first screen line that has some
 18597              glyphs coming from the buffer text.  */
 18598           do {
 18599             skip = move_it_in_display_line_to (&it, ZV, -1, MOVE_TO_POS);
 18600             if (skip != MOVE_NEWLINE_OR_CR
 18601                 || IT_CHARPOS (it) != PT
 18602                 || it.method == GET_FROM_BUFFER)
 18603               break;
 18604             vpos = it.vpos;
 18605             move_it_to (&it, -1, -1, -1, vpos + 1, MOVE_TO_VPOS);
 18606           } while (it.vpos > vpos);
 18607 
 18608           dy = it.current_y - y1;
 18609 
 18610           if (dy > scroll_max)
 18611             return SCROLLING_FAILED;
 18612 
 18613           if (dy > 0)
 18614             scroll_down_p = true;
 18615         }
 18616     }
 18617 
 18618   if (scroll_down_p)
 18619     {
 18620       /* Point is in or below the bottom scroll margin, so move the
 18621          window start down.  If scrolling conservatively, move it just
 18622          enough down to make point visible.  If scroll_step is set,
 18623          move it down by scroll_step.  */
 18624       if (arg_scroll_conservatively)
 18625         amount_to_scroll
 18626           = min (max (dy, frame_line_height),
 18627                  frame_line_height * arg_scroll_conservatively);
 18628       else if (scroll_step || temp_scroll_step)
 18629         amount_to_scroll = scroll_max;
 18630       else
 18631         {
 18632           aggressive = BVAR (current_buffer, scroll_up_aggressively);
 18633           height = WINDOW_BOX_TEXT_HEIGHT (w);
 18634           if (NUMBERP (aggressive))
 18635             {
 18636               double float_amount = XFLOATINT (aggressive) * height;
 18637               int aggressive_scroll = float_amount;
 18638               if (aggressive_scroll == 0 && float_amount > 0)
 18639                 aggressive_scroll = 1;
 18640               /* Don't let point enter the scroll margin near top of
 18641                  the window.  This could happen if the value of
 18642                  scroll_up_aggressively is too large and there are
 18643                  non-zero margins, because scroll_up_aggressively
 18644                  means put point that fraction of window height
 18645                  _from_the_bottom_margin_.  */
 18646               if (aggressive_scroll + 2 * this_scroll_margin > height)
 18647                 aggressive_scroll = height - 2 * this_scroll_margin;
 18648               amount_to_scroll = dy + aggressive_scroll;
 18649             }
 18650         }
 18651 
 18652       if (amount_to_scroll <= 0)
 18653         return SCROLLING_FAILED;
 18654 
 18655       start_display (&it, w, startp);
 18656       if (arg_scroll_conservatively <= scroll_limit)
 18657         move_it_vertically (&it, amount_to_scroll);
 18658       else
 18659         {
 18660           /* Extra precision for users who set scroll-conservatively
 18661              to a large number: make sure the amount we scroll
 18662              the window start is never less than amount_to_scroll,
 18663              which was computed as distance from window bottom to
 18664              point.  This matters when lines at window top and lines
 18665              below window bottom have different height.  */
 18666           struct it it1;
 18667           void *it1data = NULL;
 18668           /* We use a temporary it1 because line_bottom_y can modify
 18669              its argument, if it moves one line down; see there.  */
 18670           int start_y;
 18671 
 18672           SAVE_IT (it1, it, it1data);
 18673           start_y = line_bottom_y (&it1);
 18674           do {
 18675             RESTORE_IT (&it, &it, it1data);
 18676             move_it_by_lines (&it, 1);
 18677             SAVE_IT (it1, it, it1data);
 18678           } while (IT_CHARPOS (it) < ZV
 18679                    && line_bottom_y (&it1) - start_y < amount_to_scroll);
 18680           bidi_unshelve_cache (it1data, true);
 18681         }
 18682 
 18683       /* If STARTP is unchanged, move it down another screen line.  */
 18684       if (IT_CHARPOS (it) == CHARPOS (startp))
 18685         move_it_by_lines (&it, 1);
 18686       startp = it.current.pos;
 18687     }
 18688   else
 18689     {
 18690       struct text_pos scroll_margin_pos = startp;
 18691       int y_offset = 0;
 18692 
 18693       /* See if point is inside the scroll margin at the top of the
 18694          window.  */
 18695       if (this_scroll_margin)
 18696         {
 18697           int y_start;
 18698 
 18699           start_display (&it, w, startp);
 18700           y_start = it.current_y;
 18701           move_it_vertically (&it, this_scroll_margin);
 18702           scroll_margin_pos = it.current.pos;
 18703           /* If we didn't move enough before hitting ZV, request
 18704              additional amount of scroll, to move point out of the
 18705              scroll margin.  */
 18706           if (IT_CHARPOS (it) == ZV
 18707               && it.current_y - y_start < this_scroll_margin)
 18708             y_offset = this_scroll_margin - (it.current_y - y_start);
 18709         }
 18710 
 18711       if (PT < CHARPOS (scroll_margin_pos))
 18712         {
 18713           /* Point is in the scroll margin at the top of the window or
 18714              above what is displayed in the window.  */
 18715           int y0, y_to_move;
 18716 
 18717           /* Compute the vertical distance from PT to the scroll
 18718              margin position.  Move as far as scroll_max allows, or
 18719              one screenful, or 10 screen lines, whichever is largest.
 18720              Give up if distance is greater than scroll_max or if we
 18721              didn't reach the scroll margin position.  */
 18722           SET_TEXT_POS (pos, PT, PT_BYTE);
 18723           start_display (&it, w, pos);
 18724           y0 = it.current_y;
 18725           y_to_move = max (it.last_visible_y,
 18726                            max (scroll_max, 10 * frame_line_height));
 18727           move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
 18728                       y_to_move, -1,
 18729                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18730           dy = it.current_y - y0;
 18731           if (dy > scroll_max
 18732               || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
 18733             return SCROLLING_FAILED;
 18734 
 18735           /* Additional scroll for when ZV was too close to point.  */
 18736           dy += y_offset;
 18737 
 18738           /* Compute new window start.  */
 18739           start_display (&it, w, startp);
 18740 
 18741           if (arg_scroll_conservatively)
 18742             amount_to_scroll
 18743               = min (max (dy, frame_line_height),
 18744                      frame_line_height * arg_scroll_conservatively);
 18745           else if (scroll_step || temp_scroll_step)
 18746             amount_to_scroll = scroll_max;
 18747           else
 18748             {
 18749               aggressive = BVAR (current_buffer, scroll_down_aggressively);
 18750               height = WINDOW_BOX_TEXT_HEIGHT (w);
 18751               if (NUMBERP (aggressive))
 18752                 {
 18753                   double float_amount = XFLOATINT (aggressive) * height;
 18754                   int aggressive_scroll = float_amount;
 18755                   if (aggressive_scroll == 0 && float_amount > 0)
 18756                     aggressive_scroll = 1;
 18757                   /* Don't let point enter the scroll margin near
 18758                      bottom of the window, if the value of
 18759                      scroll_down_aggressively happens to be too
 18760                      large.  */
 18761                   if (aggressive_scroll + 2 * this_scroll_margin > height)
 18762                     aggressive_scroll = height - 2 * this_scroll_margin;
 18763                   amount_to_scroll = dy + aggressive_scroll;
 18764                 }
 18765             }
 18766 
 18767           if (amount_to_scroll <= 0)
 18768             return SCROLLING_FAILED;
 18769 
 18770           move_it_vertically_backward (&it, amount_to_scroll);
 18771           startp = it.current.pos;
 18772         }
 18773     }
 18774 
 18775   /* Run window scroll functions.  */
 18776   startp = run_window_scroll_functions (window, startp);
 18777 
 18778   /* Display the window.  Give up if new fonts are loaded, or if point
 18779      doesn't appear.  */
 18780   if (!try_window (window, startp, 0))
 18781     rc = SCROLLING_NEED_LARGER_MATRICES;
 18782   else if (w->cursor.vpos < 0)
 18783     {
 18784       clear_glyph_matrix (w->desired_matrix);
 18785       rc = SCROLLING_FAILED;
 18786     }
 18787   else
 18788     {
 18789       /* Maybe forget recorded base line for line number display.  */
 18790       if (!just_this_one_p
 18791           || current_buffer->clip_changed
 18792           || BEG_UNCHANGED < CHARPOS (startp))
 18793         w->base_line_number = 0;
 18794 
 18795       /* If cursor ends up on a partially visible line,
 18796          treat that as being off the bottom of the screen.  */
 18797       if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1,
 18798                                         false, false)
 18799           /* It's possible that the cursor is on the first line of the
 18800              buffer, which is partially obscured due to a vscroll
 18801              (Bug#7537).  In that case, avoid looping forever. */
 18802           && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
 18803         {
 18804           clear_glyph_matrix (w->desired_matrix);
 18805           ++extra_scroll_margin_lines;
 18806           goto too_near_end;
 18807         }
 18808       rc = SCROLLING_SUCCESS;
 18809     }
 18810 
 18811   return rc;
 18812 }
 18813 
 18814 
 18815 /* Compute a suitable window start for window W if display of W starts
 18816    on a continuation line.  Value is true if a new window start
 18817    was computed.
 18818 
 18819    The new window start will be computed, based on W's width, starting
 18820    from the start of the continued line.  It is the start of the
 18821    screen line with the minimum distance from the old start W->start,
 18822    which is still before point (otherwise point will definitely not
 18823    be visible in the window).  */
 18824 
 18825 static bool
 18826 compute_window_start_on_continuation_line (struct window *w)
 18827 {
 18828   struct text_pos pos, start_pos, pos_before_pt;
 18829   bool window_start_changed_p = false;
 18830 
 18831   SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 18832 
 18833   /* If window start is on a continuation line...  Window start may be
 18834      < BEGV in case there's invisible text at the start of the
 18835      buffer (M-x rmail, for example).  */
 18836   if (CHARPOS (start_pos) > BEGV
 18837       && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
 18838     {
 18839       struct it it;
 18840       struct glyph_row *row;
 18841 
 18842       /* Handle the case that the window start is out of range.  */
 18843       if (CHARPOS (start_pos) < BEGV)
 18844         SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
 18845       else if (CHARPOS (start_pos) > ZV)
 18846         SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
 18847 
 18848       /* Find the start of the continued line.  This should be fast
 18849          because find_newline is fast (newline cache).  */
 18850       row = w->desired_matrix->rows + window_wants_tab_line (w)
 18851                                     + window_wants_header_line (w);
 18852       init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
 18853                      row, DEFAULT_FACE_ID);
 18854       reseat_at_previous_visible_line_start (&it);
 18855 
 18856       /* Give up (by not using the code in the block below) and say it
 18857          takes too much time to compute a new window start, if the
 18858          line start is "too far" away from the window start.  Also,
 18859          give up if the line start is after point, as in that case
 18860          point will not be visible with any window start we
 18861          compute.  */
 18862       if (IT_CHARPOS (it) <= PT
 18863           && (CHARPOS (start_pos) - IT_CHARPOS (it)
 18864               /* PXW: Do we need upper bounds here?  */
 18865               < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w)))
 18866         {
 18867           int min_distance, distance;
 18868 
 18869           /* Move forward by display lines to find the new window
 18870              start.  If window width was enlarged, the new start can
 18871              be expected to be > the old start.  If window width was
 18872              decreased, the new window start will be < the old start.
 18873              So, we're looking for the display line start with the
 18874              minimum distance from the old window start.  */
 18875           pos_before_pt = pos = it.current.pos;
 18876           min_distance = DISP_INFINITY;
 18877           while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
 18878                  distance < min_distance)
 18879             {
 18880               min_distance = distance;
 18881               if (CHARPOS (pos) <= PT)
 18882                 pos_before_pt = pos;
 18883               pos = it.current.pos;
 18884               if (it.line_wrap == WORD_WRAP)
 18885                 {
 18886                   /* Under WORD_WRAP, move_it_by_lines is likely to
 18887                      overshoot and stop not at the first, but the
 18888                      second character from the left margin.  So in
 18889                      that case, we need a more tight control on the X
 18890                      coordinate of the iterator than move_it_by_lines
 18891                      promises in its contract.  The method is to first
 18892                      go to the last (rightmost) visible character of a
 18893                      line, then move to the leftmost character on the
 18894                      next line in a separate call.  */
 18895                   move_it_to (&it, ZV, it.last_visible_x, it.current_y, -1,
 18896                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18897                   move_it_to (&it, ZV, 0,
 18898                               it.current_y + it.max_ascent + it.max_descent, -1,
 18899                               MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 18900                 }
 18901               else
 18902                 move_it_by_lines (&it, 1);
 18903             }
 18904 
 18905           /* It makes very little sense to make the new window start
 18906              after point, as point won't be visible.  If that's what
 18907              the loop above finds, fall back on the candidate before
 18908              or at point that is closest to the old window start.  */
 18909           if (CHARPOS (pos) > PT)
 18910             pos = pos_before_pt;
 18911 
 18912           /* Set the window start there.  */
 18913           SET_MARKER_FROM_TEXT_POS (w->start, pos);
 18914           window_start_changed_p = true;
 18915         }
 18916     }
 18917 
 18918   return window_start_changed_p;
 18919 }
 18920 
 18921 
 18922 /* Try cursor movement in case text has not changed in window WINDOW,
 18923    with window start STARTP.  Value is
 18924 
 18925    CURSOR_MOVEMENT_SUCCESS if successful
 18926 
 18927    CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
 18928 
 18929    CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
 18930    display.  *SCROLL_STEP is set to true, under certain circumstances, if
 18931    we want to scroll as if scroll-step were set to 1.  See the code.
 18932 
 18933    CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
 18934    which case we have to abort this redisplay, and adjust matrices
 18935    first.  */
 18936 
 18937 enum
 18938 {
 18939   CURSOR_MOVEMENT_SUCCESS,
 18940   CURSOR_MOVEMENT_CANNOT_BE_USED,
 18941   CURSOR_MOVEMENT_MUST_SCROLL,
 18942   CURSOR_MOVEMENT_NEED_LARGER_MATRICES
 18943 };
 18944 
 18945 static int
 18946 try_cursor_movement (Lisp_Object window, struct text_pos startp,
 18947                      bool *scroll_step)
 18948 {
 18949   struct window *w = XWINDOW (window);
 18950   struct frame *f = XFRAME (w->frame);
 18951   int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
 18952 
 18953 #ifdef GLYPH_DEBUG
 18954   if (inhibit_try_cursor_movement)
 18955     return rc;
 18956 #endif
 18957 
 18958   /* Previously, there was a check for Lisp integer in the
 18959      if-statement below. Now, this field is converted to
 18960      ptrdiff_t, thus zero means invalid position in a buffer.  */
 18961   eassert (w->last_point > 0);
 18962   /* Likewise there was a check whether window_end_vpos is nil or larger
 18963      than the window.  Now window_end_vpos is int and so never nil, but
 18964      let's leave eassert to check whether it fits in the window.  */
 18965   eassert (!w->window_end_valid
 18966            || w->window_end_vpos < w->current_matrix->nrows);
 18967 
 18968   /* Handle case where text has not changed, only point, and it has
 18969      not moved off the frame.  */
 18970   if (/* Point may be in this window.  */
 18971       PT >= CHARPOS (startp)
 18972       /* Selective display hasn't changed.  */
 18973       && !current_buffer->clip_changed
 18974       /* Function force-mode-line-update is used to force a thorough
 18975          redisplay.  It sets either windows_or_buffers_changed or
 18976          update_mode_lines.  So don't take a shortcut here for these
 18977          cases.  */
 18978       && !update_mode_lines
 18979       && !windows_or_buffers_changed
 18980       && !f->cursor_type_changed
 18981       && NILP (Vshow_trailing_whitespace)
 18982       /* When display-line-numbers is in relative mode, moving point
 18983          requires to redraw the entire window.  */
 18984       && !EQ (Vdisplay_line_numbers, Qrelative)
 18985       && !EQ (Vdisplay_line_numbers, Qvisual)
 18986       /* When the current line number should be displayed in a
 18987          distinct face, moving point cannot be handled in optimized
 18988          way as below.  */
 18989       && !(!NILP (Vdisplay_line_numbers)
 18990            && NILP (Finternal_lisp_face_equal_p (Qline_number,
 18991                                                  Qline_number_current_line,
 18992                                                  w->frame)))
 18993       /* This code is not used for mini-buffer for the sake of the case
 18994          of redisplaying to replace an echo area message; since in
 18995          that case the mini-buffer contents per se are usually
 18996          unchanged.  This code is of no real use in the mini-buffer
 18997          since the handling of this_line_start_pos, etc., in redisplay
 18998          handles the same cases.  */
 18999       && !EQ (window, minibuf_window)
 19000       /* When overlay arrow is shown in current buffer, point movement
 19001          is no longer "simple", as it typically causes the overlay
 19002          arrow to move as well.  */
 19003       && !overlay_arrow_in_current_buffer_p ())
 19004     {
 19005       int this_scroll_margin, top_scroll_margin;
 19006       struct glyph_row *row = NULL;
 19007 
 19008 #ifdef GLYPH_DEBUG
 19009       debug_method_add (w, "cursor movement");
 19010 #endif
 19011 
 19012       this_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 19013 
 19014       top_scroll_margin = this_scroll_margin;
 19015       if (window_wants_tab_line (w))
 19016         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 19017       if (window_wants_header_line (w))
 19018         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 19019 
 19020       /* Start with the row the cursor was displayed during the last
 19021          not paused redisplay.  Give up if that row is not valid.  */
 19022       if (w->last_cursor_vpos < 0
 19023           || w->last_cursor_vpos >= w->current_matrix->nrows)
 19024         rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19025       else
 19026         {
 19027           row = MATRIX_ROW (w->current_matrix, w->last_cursor_vpos);
 19028           /* Skip the tab-line and header-line rows, if any.  */
 19029           if (row->tab_line_p)
 19030             ++row;
 19031           if (row->mode_line_p)
 19032             ++row;
 19033           if (!row->enabled_p)
 19034             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19035         }
 19036 
 19037       if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
 19038         {
 19039           bool scroll_p = false, must_scroll = false;
 19040           int last_y = window_text_bottom_y (w) - this_scroll_margin;
 19041 
 19042           if (PT > w->last_point)
 19043             {
 19044               /* Point has moved forward.  */
 19045               while (MATRIX_ROW_END_CHARPOS (row) < PT
 19046                      && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19047                 {
 19048                   eassert (row->enabled_p);
 19049                   ++row;
 19050                 }
 19051 
 19052               /* If the end position of a row equals the start
 19053                  position of the next row, and PT is at that position,
 19054                  we would rather display cursor in the next line.  */
 19055               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19056                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19057                      && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
 19058                      && MATRIX_ROW_START_CHARPOS (row+1) >= PT
 19059                      && !cursor_row_p (row))
 19060                 ++row;
 19061 
 19062               /* If within the scroll margin, scroll.  Note that
 19063                  MATRIX_ROW_BOTTOM_Y gives the pixel position at which
 19064                  the next line would be drawn, and that
 19065                  this_scroll_margin can be zero.  */
 19066               if (MATRIX_ROW_BOTTOM_Y (row) > last_y
 19067                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19068                   /* Line is completely visible last line in window
 19069                      and PT is to be set in the next line.  */
 19070                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19071                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19072                       && !row->ends_at_zv_p
 19073                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19074                 scroll_p = true;
 19075             }
 19076           else if (PT < w->last_point)
 19077             {
 19078               /* Cursor has to be moved backward.  Note that PT >=
 19079                  CHARPOS (startp) because of the outer if-statement.  */
 19080               struct glyph_row *row0 = row;
 19081 
 19082               while (!row->mode_line_p
 19083                      && (MATRIX_ROW_START_CHARPOS (row) > PT
 19084                          || (MATRIX_ROW_START_CHARPOS (row) == PT
 19085                              && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
 19086                                  || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
 19087                                      row > w->current_matrix->rows
 19088                                      && (row-1)->ends_in_newline_from_string_p))))
 19089                      && (row->y > top_scroll_margin
 19090                          || CHARPOS (startp) == BEGV))
 19091                 {
 19092                   eassert (row->enabled_p);
 19093                   --row;
 19094                 }
 19095 
 19096               /* With bidi-reordered rows we can have buffer positions
 19097                  _decrease_ when going down by rows.  If we haven't
 19098                  found our row in the loop above, give it another try
 19099                  now going in the other direction from the original row.  */
 19100               if (!(MATRIX_ROW_START_CHARPOS (row) <= PT
 19101                     && PT <= MATRIX_ROW_END_CHARPOS (row))
 19102                   && row0->continued_p)
 19103                 {
 19104                   row = row0;
 19105                   while (MATRIX_ROW_START_CHARPOS (row) > PT
 19106                          && MATRIX_ROW_BOTTOM_Y (row) < last_y)
 19107                     {
 19108                       eassert (row->enabled_p);
 19109                       ++row;
 19110                     }
 19111                 }
 19112 
 19113               /* Consider the following case: Window starts at BEGV,
 19114                  there is invisible, intangible text at BEGV, so that
 19115                  display starts at some point START > BEGV.  It can
 19116                  happen that we are called with PT somewhere between
 19117                  BEGV and START.  Try to handle that case.  */
 19118               if (row < w->current_matrix->rows
 19119                   || row->mode_line_p)
 19120                 {
 19121                   row = w->current_matrix->rows;
 19122                   /* Skip the tab-line and header-line rows, if any.  */
 19123                   if (row->tab_line_p)
 19124                     ++row;
 19125                   if (row->mode_line_p)
 19126                     ++row;
 19127                 }
 19128 
 19129               /* Due to newlines in overlay strings, we may have to
 19130                  skip forward over overlay strings.  */
 19131               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19132                      && MATRIX_ROW_END_CHARPOS (row) == PT
 19133                      && !cursor_row_p (row))
 19134                 ++row;
 19135 
 19136               /* If within the scroll margin, either the top one or
 19137                  the bottom one, scroll.  */
 19138               if ((row->y < top_scroll_margin
 19139                    && CHARPOS (startp) != BEGV)
 19140                   || MATRIX_ROW_BOTTOM_Y (row) > last_y
 19141                   || PT > MATRIX_ROW_END_CHARPOS (row)
 19142                   || (MATRIX_ROW_BOTTOM_Y (row) == last_y
 19143                       && PT == MATRIX_ROW_END_CHARPOS (row)
 19144                       && !row->ends_at_zv_p
 19145                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 19146                 scroll_p = true;
 19147             }
 19148           else
 19149             {
 19150               /* Cursor did not move.  So don't scroll even if cursor line
 19151                  is partially visible, as it was so before.  */
 19152                  rc = CURSOR_MOVEMENT_SUCCESS;
 19153             }
 19154 
 19155           if ((PT < MATRIX_ROW_START_CHARPOS (row)
 19156                && (row == MATRIX_FIRST_TEXT_ROW (w->current_matrix)
 19157                    /* Don't give up if point is inside invisible text
 19158                       at the beginning of its glyph row.  */
 19159                    || (MATRIX_ROW_END_CHARPOS (row-1)
 19160                        == MATRIX_ROW_START_CHARPOS (row))))
 19161               || PT > MATRIX_ROW_END_CHARPOS (row))
 19162             {
 19163               /* if PT is not in the glyph row, give up.  */
 19164               rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19165               must_scroll = true;
 19166             }
 19167           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19168                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19169             {
 19170               struct glyph_row *row1;
 19171 
 19172               /* If rows are bidi-reordered and point moved, back up
 19173                  until we find a row that does not belong to a
 19174                  continuation line.  This is because we must consider
 19175                  all rows of a continued line as candidates for the
 19176                  new cursor positioning, since row start and end
 19177                  positions change non-linearly with vertical position
 19178                  in such rows.  */
 19179               /* FIXME: Revisit this when glyph ``spilling'' in
 19180                  continuation lines' rows is implemented for
 19181                  bidi-reordered rows.  */
 19182               for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 19183                    MATRIX_ROW_CONTINUATION_LINE_P (row);
 19184                    --row)
 19185                 {
 19186                   /* If we hit the beginning of the displayed portion
 19187                      without finding the first row of a continued
 19188                      line, give up.  */
 19189                   if (row <= row1)
 19190                     {
 19191                       rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19192                       break;
 19193                     }
 19194                   eassert (row->enabled_p);
 19195                 }
 19196             }
 19197           if (must_scroll)
 19198             ;
 19199           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19200               && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
 19201               /* Make sure this isn't a header line nor a tab-line by
 19202                  any chance, since then MATRIX_ROW_PARTIALLY_VISIBLE_P
 19203                  might yield true.  */
 19204               && !row->mode_line_p
 19205               && !cursor_row_fully_visible_p (w, true, true, true))
 19206             {
 19207               if (PT == MATRIX_ROW_END_CHARPOS (row)
 19208                   && !row->ends_at_zv_p
 19209                   && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 19210                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19211               else if (row->height > window_box_height (w))
 19212                 {
 19213                   /* If we end up in a partially visible line, let's
 19214                      make it fully visible, except when it's taller
 19215                      than the window, in which case we can't do much
 19216                      about it.  */
 19217                   *scroll_step = true;
 19218                   rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19219                 }
 19220               else
 19221                 {
 19222                   set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 19223                   if (!cursor_row_fully_visible_p (w, false, true, false))
 19224                     rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19225                   else
 19226                     rc = CURSOR_MOVEMENT_SUCCESS;
 19227                 }
 19228             }
 19229           else if (scroll_p)
 19230             rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19231           else if (rc != CURSOR_MOVEMENT_SUCCESS
 19232                    && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 19233             {
 19234               /* With bidi-reordered rows, there could be more than
 19235                  one candidate row whose start and end positions
 19236                  occlude point.  We need to let set_cursor_from_row
 19237                  find the best candidate.  */
 19238               /* FIXME: Revisit this when glyph ``spilling'' in
 19239                  continuation lines' rows is implemented for
 19240                  bidi-reordered rows.  */
 19241               bool rv = false;
 19242               bool pt_invis = false;
 19243               Lisp_Object val = get_char_property_and_overlay (make_fixnum (PT),
 19244                                                                Qinvisible,
 19245                                                                Qnil, NULL);
 19246 
 19247               if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 19248                 pt_invis = true;
 19249 
 19250               do
 19251                 {
 19252                   bool at_zv_p = false, exact_match_p = false;
 19253 
 19254                   /* If point is in invisible text, we cannot assume
 19255                      it must be after row's start position, since the
 19256                      row could have invisible text at its beginning
 19257                      where point is located.  */
 19258                   if ((pt_invis || MATRIX_ROW_START_CHARPOS (row) <= PT)
 19259                       && PT <= MATRIX_ROW_END_CHARPOS (row)
 19260                       && cursor_row_p (row))
 19261                     rv |= set_cursor_from_row (w, row, w->current_matrix,
 19262                                                0, 0, 0, 0);
 19263                   /* As soon as we've found the exact match for point,
 19264                      or the first suitable row whose ends_at_zv_p flag
 19265                      is set, we are done.  */
 19266                   if (rv)
 19267                     {
 19268                       at_zv_p = MATRIX_ROW (w->current_matrix,
 19269                                             w->cursor.vpos)->ends_at_zv_p;
 19270                       if (!at_zv_p
 19271                           && w->cursor.hpos >= 0
 19272                           && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
 19273                                                                w->cursor.vpos))
 19274                         {
 19275                           struct glyph_row *candidate =
 19276                             MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 19277                           struct glyph *g =
 19278                             candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
 19279                           ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
 19280 
 19281                           exact_match_p =
 19282                             (BUFFERP (g->object) && g->charpos == PT)
 19283                             || (NILP (g->object)
 19284                                 && (g->charpos == PT
 19285                                     || (g->charpos == 0 && endpos - 1 == PT)));
 19286                           /* Perhaps the point position is inside
 19287                              invisible text?  In that case, we trust
 19288                              'set_cursor_from_row' to do its job and
 19289                              find the best position for the cursor.  */
 19290                           if (!exact_match_p && pt_invis)
 19291                             exact_match_p = true;
 19292                         }
 19293                       if (at_zv_p || exact_match_p)
 19294                         {
 19295                           rc = CURSOR_MOVEMENT_SUCCESS;
 19296                           break;
 19297                         }
 19298                     }
 19299                   if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
 19300                     break;
 19301                   ++row;
 19302                 }
 19303               while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
 19304                        || row->continued_p)
 19305                       && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
 19306                      || (MATRIX_ROW_START_CHARPOS (row) == PT
 19307                          && MATRIX_ROW_BOTTOM_Y (row) < last_y));
 19308               /* If we didn't find any candidate rows, or exited the
 19309                  loop before all the candidates were examined, signal
 19310                  to the caller that this method failed.  */
 19311               if (rc != CURSOR_MOVEMENT_SUCCESS
 19312                   && !(rv
 19313                        && !MATRIX_ROW_CONTINUATION_LINE_P (row)
 19314                        && !row->continued_p))
 19315                 rc = CURSOR_MOVEMENT_MUST_SCROLL;
 19316               else if (rv)
 19317                 rc = CURSOR_MOVEMENT_SUCCESS;
 19318             }
 19319           else
 19320             {
 19321               do
 19322                 {
 19323                   if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
 19324                     {
 19325                       rc = CURSOR_MOVEMENT_SUCCESS;
 19326                       break;
 19327                     }
 19328                   ++row;
 19329                 }
 19330               while (MATRIX_ROW_BOTTOM_Y (row) < last_y
 19331                      && MATRIX_ROW_START_CHARPOS (row) == PT
 19332                      && cursor_row_p (row));
 19333             }
 19334         }
 19335     }
 19336 
 19337   return rc;
 19338 }
 19339 
 19340 
 19341 void
 19342 set_vertical_scroll_bar (struct window *w)
 19343 {
 19344   ptrdiff_t start, end, whole;
 19345 
 19346   /* Calculate the start and end positions for the current window.
 19347      At some point, it would be nice to choose between scrollbars
 19348      which reflect the whole buffer size, with special markers
 19349      indicating narrowing, and scrollbars which reflect only the
 19350      visible region.
 19351 
 19352      Note that mini-buffers sometimes aren't displaying any text.  */
 19353   if (!MINI_WINDOW_P (w)
 19354       || (w == XWINDOW (minibuf_window)
 19355           && NILP (echo_area_buffer[0])))
 19356     {
 19357       struct buffer *buf = XBUFFER (w->contents);
 19358 
 19359       whole = BUF_ZV (buf) - BUF_BEGV (buf);
 19360       start = marker_position (w->start) - BUF_BEGV (buf);
 19361       end = BUF_Z (buf) - w->window_end_pos - BUF_BEGV (buf);
 19362 
 19363       /* If w->window_end_pos cannot be trusted, recompute it "the
 19364          hard way".  But don't bother to be too accurate when
 19365          long-line shortcuts are in effect.  */
 19366       if (!w->window_end_valid && !buf->long_line_optimizations_p)
 19367         {
 19368           struct it it;
 19369           struct text_pos start_pos;
 19370           struct buffer *obuf = current_buffer;
 19371           /* When we display the scroll bar of a mini-window,
 19372              current_buffer is not guaranteed to be the mini-window's
 19373              buffer, see the beginning of redisplay_window.  */
 19374           set_buffer_internal_1 (XBUFFER (w->contents));
 19375           SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
 19376           start_display (&it, w, start_pos);
 19377           move_it_to (&it, -1, it.last_visible_x, window_box_height (w), -1,
 19378                       MOVE_TO_X | MOVE_TO_Y);
 19379           end -= (BUF_Z (buf) - IT_CHARPOS (it)) - w->window_end_pos;
 19380           set_buffer_internal_1 (obuf);
 19381         }
 19382 
 19383       if (end < start)
 19384         end = start;
 19385       if (whole < (end - start))
 19386         whole = end - start;
 19387     }
 19388   else
 19389     start = end = whole = 0;
 19390 
 19391   /* Indicate what this scroll bar ought to be displaying now.  */
 19392   if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19393     (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
 19394       (w, end - start, whole, start);
 19395 }
 19396 
 19397 
 19398 void
 19399 set_horizontal_scroll_bar (struct window *w)
 19400 {
 19401   int start, end, whole, portion;
 19402 
 19403   if (!MINI_WINDOW_P (w) || EQ (w->horizontal_scroll_bar_type, Qbottom))
 19404     {
 19405       struct buffer *b = XBUFFER (w->contents);
 19406       struct buffer *old_buffer = NULL;
 19407       struct it it;
 19408       struct text_pos startp;
 19409 
 19410       if (b != current_buffer)
 19411         {
 19412           old_buffer = current_buffer;
 19413           set_buffer_internal (b);
 19414         }
 19415 
 19416       SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19417       start_display (&it, w, startp);
 19418       it.last_visible_x = INT_MAX;
 19419       whole = move_it_to (&it, -1, INT_MAX, window_box_height (w), -1,
 19420                           MOVE_TO_X | MOVE_TO_Y);
 19421       /* whole = move_it_to (&it, w->window_end_pos, INT_MAX,
 19422                           window_box_height (w), -1,
 19423                           MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 19424 
 19425       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
 19426       end = start + window_box_width (w, TEXT_AREA);
 19427       portion = end - start;
 19428       /* After enlarging a horizontally scrolled window such that it
 19429          gets at least as wide as the text it contains, make sure that
 19430          the thumb doesn't fill the entire scroll bar so we can still
 19431          drag it back to see the entire text.  */
 19432       whole = max (whole, end);
 19433 
 19434       if (it.bidi_p)
 19435         {
 19436           Lisp_Object pdir;
 19437 
 19438           pdir = Fcurrent_bidi_paragraph_direction (Qnil);
 19439           if (EQ (pdir, Qright_to_left))
 19440             {
 19441               start = whole - end;
 19442               end = start + portion;
 19443             }
 19444         }
 19445 
 19446       if (old_buffer)
 19447         set_buffer_internal (old_buffer);
 19448     }
 19449   else
 19450     start = end = whole = portion = 0;
 19451 
 19452   w->hscroll_whole = whole;
 19453 
 19454   /* Indicate what this scroll bar ought to be displaying now.  */
 19455   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19456     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
 19457       (w, portion, whole, start);
 19458 }
 19459 
 19460 /* Subroutine of redisplay_window, to determine whether a window-start
 19461    point STARTP of WINDOW should be rejected.  */
 19462 static bool
 19463 window_start_acceptable_p (Lisp_Object window, ptrdiff_t startp)
 19464 {
 19465   if (!make_window_start_visible)
 19466     return true;
 19467 
 19468   struct window *w = XWINDOW (window);
 19469   struct frame *f = XFRAME (w->frame);
 19470   Lisp_Object startpos = make_fixnum (startp);
 19471   Lisp_Object invprop, disp_spec;
 19472   struct text_pos ignored;
 19473 
 19474   /* Is STARTP in invisible text?  */
 19475   if ((invprop = Fget_char_property (startpos, Qinvisible, window)),
 19476       TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19477     return false;
 19478 
 19479   /* Is STARTP covered by a replacing 'display' property?  */
 19480   if (!NILP (disp_spec = Fget_char_property (startpos, Qdisplay, window))
 19481       && handle_display_spec (NULL, disp_spec, Qnil, Qnil, &ignored, startp,
 19482                               FRAME_WINDOW_P (f)) > 0)
 19483     return false;
 19484 
 19485   return true;
 19486 }
 19487 
 19488 DEFUN ("long-line-optimizations-p", Flong_line_optimizations_p, Slong_line_optimizations_p,
 19489        0, 0, 0,
 19490        doc: /* Return non-nil if long-line optimizations are in effect in current buffer.
 19491 See `long-line-threshold' and `large-hscroll-threshold' for what these
 19492 optimizations mean and when they are in effect.  */)
 19493   (void)
 19494 {
 19495   return current_buffer->long_line_optimizations_p ? Qt : Qnil;
 19496 }
 19497 
 19498 /* Redisplay leaf window WINDOW.  JUST_THIS_ONE_P means only
 19499    selected_window is redisplayed.
 19500 
 19501    We can return without actually redisplaying the window if fonts has been
 19502    changed on window's frame.  In that case, redisplay_internal will retry.
 19503 
 19504    As one of the important parts of redisplaying a window, we need to
 19505    decide whether the previous window-start position (stored in the
 19506    window's w->start marker position) is still valid, and if it isn't,
 19507    recompute it.  Some details about that:
 19508 
 19509     . The previous window-start could be in a continuation line, in
 19510       which case we need to recompute it when the window width
 19511       changes.  See compute_window_start_on_continuation_line and its
 19512       call below.
 19513 
 19514     . The text that changed since last redisplay could include the
 19515       previous window-start position.  In that case, we try to salvage
 19516       what we can from the current glyph matrix by calling
 19517       try_scrolling, which see.
 19518 
 19519     . Some Emacs command could force us to use a specific window-start
 19520       position by setting the window's force_start flag, or gently
 19521       propose doing that by setting the window's optional_new_start
 19522       flag.  In these cases, we try using the specified start point if
 19523       that succeeds (i.e. the window desired matrix is successfully
 19524       recomputed, and point location is within the window).  In case
 19525       of optional_new_start, we first check if the specified start
 19526       position is feasible, i.e. if it will allow point to be
 19527       displayed in the window.  If using the specified start point
 19528       fails, e.g., if new fonts are needed to be loaded, we abort the
 19529       redisplay cycle and leave it up to the next cycle to figure out
 19530       things.
 19531 
 19532     . Note that the window's force_start flag is sometimes set by
 19533       redisplay itself, when it decides that the previous window start
 19534       point is fine and should be kept.  Search for "goto force_start"
 19535       below to see the details.  Like the values of window-start
 19536       specified outside of redisplay, these internally-deduced values
 19537       are tested for feasibility, and ignored if found to be
 19538       unfeasible.
 19539 
 19540     . Note that the function try_window, used to completely redisplay
 19541       a window, accepts the window's start point as its argument.
 19542       This is used several times in the redisplay code to control
 19543       where the window start will be, according to user options such
 19544       as scroll-conservatively, and also to ensure the screen line
 19545       showing point will be fully (as opposed to partially) visible on
 19546       display.  */
 19547 
 19548 static void
 19549 redisplay_window (Lisp_Object window, bool just_this_one_p)
 19550 {
 19551   struct window *w = XWINDOW (window);
 19552   struct frame *f = XFRAME (w->frame);
 19553   struct buffer *buffer = XBUFFER (w->contents);
 19554   struct buffer *old = current_buffer;
 19555   struct text_pos lpoint, opoint, startp;
 19556   bool update_mode_line;
 19557   int tem;
 19558   struct it it;
 19559   /* Record it now because it's overwritten.  */
 19560   bool current_matrix_up_to_date_p = false;
 19561   bool used_current_matrix_p = false;
 19562   /* This is less strict than current_matrix_up_to_date_p.
 19563      It indicates that the buffer contents and narrowing are unchanged.  */
 19564   bool buffer_unchanged_p = false;
 19565   bool temp_scroll_step = false;
 19566   specpdl_ref count = SPECPDL_INDEX ();
 19567   int rc;
 19568   int centering_position = -1;
 19569   bool last_line_misfit = false;
 19570   ptrdiff_t beg_unchanged, end_unchanged;
 19571   int frame_line_height, margin;
 19572   bool use_desired_matrix;
 19573   void *itdata = NULL;
 19574 
 19575   SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19576   opoint = lpoint;
 19577 
 19578 #ifdef GLYPH_DEBUG
 19579   *w->desired_matrix->method = 0;
 19580 #endif
 19581 
 19582   if (!just_this_one_p && needs_no_redisplay (w))
 19583     return;
 19584 
 19585   /* Make sure that both W's markers are valid.  */
 19586   eassert (XMARKER (w->start)->buffer == buffer);
 19587   eassert (XMARKER (w->pointm)->buffer == buffer);
 19588 
 19589   reconsider_clip_changes (w);
 19590   frame_line_height = default_line_pixel_height (w);
 19591   margin = window_scroll_margin (w, MARGIN_IN_LINES);
 19592 
 19593 
 19594   /* Has the mode line to be updated?  */
 19595   update_mode_line = (w->update_mode_line
 19596                       || update_mode_lines
 19597                       || buffer->clip_changed
 19598                       || buffer->prevent_redisplay_optimizations_p);
 19599 
 19600   if (!just_this_one_p)
 19601     /* If `just_this_one_p' is set, we apparently set must_be_updated_p more
 19602        cleverly elsewhere.  */
 19603     w->must_be_updated_p = true;
 19604 
 19605   if (MINI_WINDOW_P (w))
 19606     {
 19607       if (w == XWINDOW (echo_area_window)
 19608           && !NILP (echo_area_buffer[0]))
 19609         {
 19610           if (update_mode_line)
 19611             /* We may have to update a tty frame's menu bar or a
 19612                tool-bar.  Example `M-x C-h C-h C-g'.  */
 19613             goto finish_menu_bars;
 19614           else
 19615             /* We've already displayed the echo area glyphs in this window.  */
 19616             goto finish_scroll_bars;
 19617         }
 19618       else if ((w != XWINDOW (minibuf_window)
 19619                 || minibuf_level == 0)
 19620                /* When buffer is nonempty, redisplay window normally.  */
 19621                && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
 19622                /* Quail displays non-mini buffers in minibuffer window.
 19623                   In that case, redisplay the window normally.  */
 19624                && !NILP (Fmemq (w->contents, Vminibuffer_list)))
 19625         {
 19626           /* W is a mini-buffer window, but it's not active, so clear
 19627              it.  */
 19628           int yb = window_text_bottom_y (w);
 19629           struct glyph_row *row;
 19630           int y;
 19631 
 19632           for (y = 0, row = w->desired_matrix->rows;
 19633                y < yb;
 19634                y += row->height, ++row)
 19635             blank_row (w, row, y);
 19636           goto finish_scroll_bars;
 19637         }
 19638       else if (minibuf_level >= 1)
 19639         {
 19640           /* We could have a message produced by set-minibuffer-message
 19641              displayed in the mini-window as an overlay, so resize the
 19642              mini-window if needed.  */
 19643           resize_mini_window (w, false);
 19644         }
 19645 
 19646       clear_glyph_matrix (w->desired_matrix);
 19647     }
 19648 
 19649   /* Otherwise set up data on this window; select its buffer and point
 19650      value.  */
 19651   /* Really select the buffer, for the sake of buffer-local
 19652      variables.  */
 19653   set_buffer_internal_1 (XBUFFER (w->contents));
 19654 
 19655   current_matrix_up_to_date_p
 19656     = (w->window_end_valid
 19657        && !current_buffer->clip_changed
 19658        && !current_buffer->prevent_redisplay_optimizations_p
 19659        && !window_outdated (w)
 19660        && !composition_break_at_point
 19661        && !hscrolling_current_line_p (w));
 19662 
 19663   beg_unchanged = BEG_UNCHANGED;
 19664   end_unchanged = END_UNCHANGED;
 19665 
 19666   SET_TEXT_POS (opoint, PT, PT_BYTE);
 19667 
 19668   specbind (Qinhibit_point_motion_hooks, Qt);
 19669 
 19670   buffer_unchanged_p
 19671     = (w->window_end_valid
 19672        && !current_buffer->clip_changed
 19673        && !window_outdated (w));
 19674 
 19675   /* When windows_or_buffers_changed is non-zero, we can't rely
 19676      on the window end being valid, so set it to zero there.  */
 19677   if (windows_or_buffers_changed)
 19678     {
 19679       /* If window starts on a continuation line, maybe adjust the
 19680          window start in case the window's width changed.  */
 19681       if (XMARKER (w->start)->buffer == current_buffer)
 19682         compute_window_start_on_continuation_line (w);
 19683 
 19684       w->window_end_valid = false;
 19685       /* If so, we also can't rely on current matrix
 19686          and should not fool try_cursor_movement below.  */
 19687       current_matrix_up_to_date_p = false;
 19688     }
 19689 
 19690   /* Some sanity checks.  */
 19691   CHECK_WINDOW_END (w);
 19692   if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
 19693     emacs_abort ();
 19694   if (BYTEPOS (opoint) < CHARPOS (opoint))
 19695     emacs_abort ();
 19696 
 19697   if (mode_line_update_needed (w))
 19698     update_mode_line = true;
 19699 
 19700   /* Point refers normally to the selected window.  For any other
 19701      window, set up appropriate value.  */
 19702   if (!EQ (window, selected_window))
 19703     {
 19704       ptrdiff_t new_pt = marker_position (w->pointm);
 19705       ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
 19706 
 19707       if (new_pt < BEGV)
 19708         {
 19709           new_pt = BEGV;
 19710           new_pt_byte = BEGV_BYTE;
 19711           set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
 19712         }
 19713       else if (new_pt > (ZV - 1))
 19714         {
 19715           new_pt = ZV;
 19716           new_pt_byte = ZV_BYTE;
 19717           set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
 19718         }
 19719 
 19720       /* We don't use SET_PT so that the point-motion hooks don't run.  */
 19721       TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
 19722     }
 19723 
 19724   /* If any of the character widths specified in the display table
 19725      have changed, invalidate the width run cache.  It's true that
 19726      this may be a bit late to catch such changes, but the rest of
 19727      redisplay goes (non-fatally) haywire when the display table is
 19728      changed, so why should we worry about doing any better?  */
 19729   if (current_buffer->width_run_cache
 19730       || (current_buffer->base_buffer
 19731           && current_buffer->base_buffer->width_run_cache))
 19732     {
 19733       struct Lisp_Char_Table *disptab = buffer_display_table ();
 19734 
 19735       if (! disptab_matches_widthtab
 19736           (disptab, XVECTOR (BVAR (current_buffer, width_table))))
 19737         {
 19738           struct buffer *buf = current_buffer;
 19739 
 19740           if (buf->base_buffer)
 19741             buf = buf->base_buffer;
 19742           invalidate_region_cache (buf, buf->width_run_cache, BEG, Z);
 19743           recompute_width_table (current_buffer, disptab);
 19744         }
 19745     }
 19746 
 19747   /* Check whether the buffer to be displayed contains long lines.  */
 19748   if (!NILP (Vlong_line_threshold)
 19749       && !current_buffer->long_line_optimizations_p
 19750       && (CHARS_MODIFF - UNCHANGED_MODIFIED > 8
 19751           || current_buffer->clip_changed))
 19752     {
 19753       ptrdiff_t cur, next, found, max = 0, threshold;
 19754       threshold = XFIXNUM (Vlong_line_threshold);
 19755       for (cur = BEGV; cur < ZV; cur = next)
 19756         {
 19757           next = find_newline1 (cur, CHAR_TO_BYTE (cur), 0, -1, 1,
 19758                                 &found, NULL, true);
 19759           if (next - cur > max) max = next - cur;
 19760           if (!found || max > threshold) break;
 19761         }
 19762       if (max > threshold)
 19763         current_buffer->long_line_optimizations_p = true;
 19764     }
 19765 
 19766   /* If window-start is screwed up, choose a new one.  */
 19767   if (XMARKER (w->start)->buffer != current_buffer)
 19768     goto recenter;
 19769 
 19770   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 19771 
 19772   /* If someone specified a new starting point but did not insist,
 19773      check whether it can be used.  */
 19774   if ((w->optional_new_start || window_frozen_p (w))
 19775       && CHARPOS (startp) >= BEGV
 19776       && CHARPOS (startp) <= ZV)
 19777     {
 19778       ptrdiff_t it_charpos;
 19779 
 19780       w->optional_new_start = false;
 19781       if (!w->force_start)
 19782         {
 19783           start_display (&it, w, startp);
 19784           move_it_to (&it, PT, 0, it.last_visible_y, -1,
 19785                       MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
 19786           /* Record IT's position now, since line_bottom_y might
 19787              change that.  */
 19788           it_charpos = IT_CHARPOS (it);
 19789           /* Make sure we set the force_start flag only if the cursor
 19790              row will be fully visible.  Otherwise, the code under
 19791              force_start label below will try to move point back into
 19792              view, which is not what the code which sets
 19793              optional_new_start wants.  */
 19794           if (it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
 19795             {
 19796               if (it_charpos == PT)
 19797                 w->force_start = true;
 19798               /* IT may overshoot PT if text at PT is invisible.  */
 19799               else if (it_charpos > PT && CHARPOS (startp) <= PT)
 19800                 w->force_start = true;
 19801 #ifdef GLYPH_DEBUG
 19802               if (w->force_start)
 19803                 {
 19804                   if (window_frozen_p (w))
 19805                     debug_method_add (w, "set force_start from frozen window start");
 19806                   else
 19807                     debug_method_add (w, "set force_start from optional_new_start");
 19808                 }
 19809 #endif
 19810             }
 19811         }
 19812     }
 19813 
 19814  force_start:
 19815 
 19816   /* Handle case where place to start displaying has been specified,
 19817      unless the specified location is outside the accessible range.  */
 19818   if (w->force_start)
 19819     {
 19820       /* We set this later on if we have to adjust point.  */
 19821       int new_vpos = -1;
 19822 
 19823       w->force_start = false;
 19824 
 19825       /* The vscroll should be preserved in this case, since
 19826          `pixel-scroll-precision-mode' must continue working normally
 19827          when a mini-window is resized.  (bug#55312) */
 19828       if (!w->preserve_vscroll_p || !window_frozen_p (w))
 19829         w->vscroll = 0;
 19830 
 19831       w->preserve_vscroll_p = false;
 19832       w->window_end_valid = false;
 19833 
 19834       /* Forget any recorded base line for line number display.  */
 19835       if (!buffer_unchanged_p)
 19836         w->base_line_number = 0;
 19837 
 19838       /* Redisplay the mode line.  Select the buffer properly for that.
 19839          Also, run the hook window-scroll-functions
 19840          because we have scrolled.  */
 19841       /* Note, we do this after clearing force_start because
 19842          if there's an error, it is better to forget about force_start
 19843          than to get into an infinite loop calling the hook functions
 19844          and having them get more errors.  */
 19845       if (!update_mode_line
 19846           || ! NILP (Vwindow_scroll_functions))
 19847         {
 19848           update_mode_line = true;
 19849           w->update_mode_line = true;
 19850           startp = run_window_scroll_functions (window, startp);
 19851         }
 19852 
 19853       if (CHARPOS (startp) < BEGV)
 19854         SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
 19855       else if (CHARPOS (startp) > ZV)
 19856         SET_TEXT_POS (startp, ZV, ZV_BYTE);
 19857 
 19858       /* Reject the specified start location if it is invisible, and
 19859          the buffer wants it always visible.  */
 19860       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 19861         goto ignore_start;
 19862 
 19863       /* Redisplay, then check if cursor has been set during the
 19864          redisplay.  Give up if new fonts were loaded.  */
 19865       /* We used to issue a CHECK_MARGINS argument to try_window here,
 19866          but this causes scrolling to fail when point begins inside
 19867          the scroll margin (bug#148) -- cyd  */
 19868       clear_glyph_matrix (w->desired_matrix);
 19869       if (!try_window (window, startp, 0))
 19870         {
 19871           w->force_start = true;
 19872           clear_glyph_matrix (w->desired_matrix);
 19873           goto need_larger_matrices;
 19874         }
 19875 
 19876       if (w->cursor.vpos < 0)
 19877         {
 19878           /* If point does not appear, try to move point so it does
 19879              appear.  The desired matrix has been built above, so we
 19880              can use it here.  First see if point is in invisible
 19881              text, and if so, move it to the first visible buffer
 19882              position past that.  */
 19883           struct glyph_row *r = NULL;
 19884           Lisp_Object invprop =
 19885             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 19886                                            Qnil, NULL);
 19887 
 19888           if (TEXT_PROP_MEANS_INVISIBLE (invprop) != 0)
 19889             {
 19890               ptrdiff_t alt_pt;
 19891               Lisp_Object invprop_end =
 19892                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 19893                                                    Qnil, Qnil);
 19894 
 19895               if (FIXNATP (invprop_end))
 19896                 alt_pt = XFIXNAT (invprop_end);
 19897               else
 19898                 alt_pt = ZV;
 19899               r = row_containing_pos (w, alt_pt, w->desired_matrix->rows,
 19900                                       NULL, 0);
 19901             }
 19902           if (r)
 19903             new_vpos = MATRIX_ROW_BOTTOM_Y (r);
 19904           else  /* Give up and just move to the middle of the window.  */
 19905             new_vpos = window_box_height (w) / 2;
 19906         }
 19907 
 19908       if (!cursor_row_fully_visible_p (w, false, false, false))
 19909         {
 19910           /* Point does appear, but on a line partly visible at end of window.
 19911              Move it back to a fully-visible line.  */
 19912           new_vpos = window_box_height (w);
 19913           /* But if window_box_height suggests a Y coordinate that is
 19914              not less than we already have, that line will clearly not
 19915              be fully visible, so give up and scroll the display.
 19916              This can happen when the default face uses a font whose
 19917              dimensions are different from the frame's default
 19918              font.  */
 19919           if (new_vpos >= w->cursor.y)
 19920             {
 19921               w->cursor.vpos = -1;
 19922               clear_glyph_matrix (w->desired_matrix);
 19923               goto try_to_scroll;
 19924             }
 19925         }
 19926       else if (w->cursor.vpos >= 0)
 19927         {
 19928           /* Some people insist on not letting point enter the scroll
 19929              margin, even though this part handles windows that didn't
 19930              scroll at all.  */
 19931           int pixel_margin = margin * frame_line_height;
 19932           bool tab_line = window_wants_tab_line (w);
 19933           bool header_line = window_wants_header_line (w);
 19934 
 19935           /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
 19936              below, which finds the row to move point to, advances by
 19937              the Y coordinate of the _next_ row, see the definition of
 19938              MATRIX_ROW_BOTTOM_Y.  */
 19939           if (w->cursor.vpos < margin + tab_line + header_line)
 19940             {
 19941               w->cursor.vpos = -1;
 19942               clear_glyph_matrix (w->desired_matrix);
 19943               goto try_to_scroll;
 19944             }
 19945           else
 19946             {
 19947               int window_height = window_box_height (w);
 19948 
 19949               if (tab_line)
 19950                 window_height += CURRENT_TAB_LINE_HEIGHT (w);
 19951               if (header_line)
 19952                 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
 19953               if (w->cursor.y >= window_height - pixel_margin)
 19954                 {
 19955                   w->cursor.vpos = -1;
 19956                   clear_glyph_matrix (w->desired_matrix);
 19957                   goto try_to_scroll;
 19958                 }
 19959             }
 19960         }
 19961 
 19962       /* If we need to move point for either of the above reasons,
 19963          now actually do it.  */
 19964       if (new_vpos >= 0)
 19965         {
 19966           struct glyph_row *row;
 19967 
 19968           row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
 19969           while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos
 19970                  && !row->ends_at_zv_p)
 19971             ++row;
 19972 
 19973           TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
 19974                             MATRIX_ROW_START_BYTEPOS (row));
 19975 
 19976           if (w != XWINDOW (selected_window))
 19977             set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
 19978           else if (current_buffer == old)
 19979             SET_TEXT_POS (lpoint, PT, PT_BYTE);
 19980 
 19981           set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 19982 
 19983           /* Re-run pre-redisplay-function so it can update the region
 19984              according to the new position of point.  */
 19985           /* Other than the cursor, w's redisplay is done so we can set its
 19986              redisplay to false.  Also the buffer's redisplay can be set to
 19987              false, since propagate_buffer_redisplay should have already
 19988              propagated its info to `w' anyway.  */
 19989           w->redisplay = false;
 19990           XBUFFER (w->contents)->text->redisplay = false;
 19991           safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
 19992 
 19993           if (w->redisplay || XBUFFER (w->contents)->text->redisplay
 19994               || ((EQ (Vdisplay_line_numbers, Qrelative)
 19995                    || EQ (Vdisplay_line_numbers, Qvisual))
 19996                   && row != MATRIX_FIRST_TEXT_ROW (w->desired_matrix)))
 19997             {
 19998               /* Either pre-redisplay-function made changes (e.g. move
 19999                  the region), or we moved point in a window that is
 20000                  under display-line-numbers = relative mode.  We need
 20001                  another round of redisplay.  */
 20002               clear_glyph_matrix (w->desired_matrix);
 20003               if (!try_window (window, startp, 0))
 20004                 goto need_larger_matrices;
 20005             }
 20006         }
 20007       if (w->cursor.vpos < 0
 20008           || !cursor_row_fully_visible_p (w, false, false, false))
 20009         {
 20010           clear_glyph_matrix (w->desired_matrix);
 20011           goto try_to_scroll;
 20012         }
 20013 
 20014 #ifdef GLYPH_DEBUG
 20015       debug_method_add (w, "forced window start");
 20016 #endif
 20017       goto done;
 20018     }
 20019 
 20020  ignore_start:
 20021 
 20022   /* Handle case where text has not changed, only point, and it has
 20023      not moved off the frame, and we are not retrying after hscroll.
 20024      (current_matrix_up_to_date_p is true when retrying.)  */
 20025   if (current_matrix_up_to_date_p
 20026       && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
 20027           rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
 20028     {
 20029       switch (rc)
 20030         {
 20031         case CURSOR_MOVEMENT_SUCCESS:
 20032           used_current_matrix_p = true;
 20033           goto done;
 20034 
 20035         case CURSOR_MOVEMENT_MUST_SCROLL:
 20036           goto try_to_scroll;
 20037 
 20038         default:
 20039           emacs_abort ();
 20040         }
 20041     }
 20042   /* If current starting point was originally the beginning of a line
 20043      but no longer is, or if the starting point is invisible but the
 20044      buffer wants it always visible, find a new starting point.  */
 20045   else if (w->start_at_line_beg
 20046            && ((CHARPOS (startp) > BEGV
 20047                 && FETCH_BYTE (BYTEPOS (startp) - 1) != '\n')
 20048                || (CHARPOS (startp) >= BEGV
 20049                    && CHARPOS (startp) <= ZV
 20050                    && !window_start_acceptable_p (window, CHARPOS (startp)))))
 20051     {
 20052 #ifdef GLYPH_DEBUG
 20053       debug_method_add (w, "recenter 1");
 20054 #endif
 20055       goto recenter;
 20056     }
 20057 
 20058   /* Try scrolling with try_window_id.  Value is > 0 if update has
 20059      been done, it is -1 if we know that the same window start will
 20060      not work.  It is 0 if unsuccessful for some other reason.  */
 20061   else if ((tem = try_window_id (w)) != 0)
 20062     {
 20063 #ifdef GLYPH_DEBUG
 20064       debug_method_add (w, "try_window_id %d", tem);
 20065 #endif
 20066 
 20067       if (f->fonts_changed)
 20068         goto need_larger_matrices;
 20069       if (tem > 0)
 20070         goto done;
 20071 
 20072       /* Otherwise try_window_id has returned -1 which means that we
 20073          don't want the alternative below this comment to execute.  */
 20074     }
 20075   else if (CHARPOS (startp) >= BEGV
 20076            && CHARPOS (startp) <= ZV
 20077            && PT >= CHARPOS (startp)
 20078            && (CHARPOS (startp) < ZV
 20079                /* Avoid starting at end of buffer.  */
 20080                || CHARPOS (startp) == BEGV
 20081                || !window_outdated (w)))
 20082     {
 20083       int d1, d2, d5, d6;
 20084       int rtop, rbot;
 20085 
 20086       /* If first window line is a continuation line, and window start
 20087          is inside the modified region, but the first change is before
 20088          current window start, we must select a new window start.
 20089 
 20090          However, if this is the result of a down-mouse event (e.g. by
 20091          extending the mouse-drag-overlay), we don't want to select a
 20092          new window start, since that would change the position under
 20093          the mouse, resulting in an unwanted mouse-movement rather
 20094          than a simple mouse-click.  */
 20095       if (!w->start_at_line_beg
 20096           && NILP (track_mouse)
 20097           && CHARPOS (startp) > BEGV
 20098           && CHARPOS (startp) > BEG + beg_unchanged
 20099           && CHARPOS (startp) <= Z - end_unchanged
 20100           /* Even if w->start_at_line_beg is nil, a new window may
 20101              start at a line_beg, since that's how set_buffer_window
 20102              sets it.  So, we need to check the return value of
 20103              compute_window_start_on_continuation_line.  (See also
 20104              bug#197).  */
 20105           && XMARKER (w->start)->buffer == current_buffer
 20106           && compute_window_start_on_continuation_line (w)
 20107           /* It doesn't make sense to force the window start like we
 20108              do at label force_start if it is already known that point
 20109              will not be fully visible in the resulting window, because
 20110              doing so will move point from its correct position
 20111              instead of scrolling the window to bring point into view.
 20112              See bug#9324.  */
 20113           && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
 20114           /* A very tall row could need more than the window height,
 20115              in which case we accept that it is partially visible.  */
 20116           && (rtop != 0) == (rbot != 0))
 20117         {
 20118           w->force_start = true;
 20119           SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20120 #ifdef GLYPH_DEBUG
 20121           debug_method_add (w, "recomputed window start in continuation line");
 20122 #endif
 20123           goto force_start;
 20124         }
 20125 
 20126       /* Don't use the same window-start if it is invisible or covered
 20127          by a replacing 'display' property and the buffer requested
 20128          the window-start to be always visible.  */
 20129       if (!window_start_acceptable_p (window, CHARPOS (startp)))
 20130         {
 20131 #ifdef GLYPH_DEBUG
 20132           debug_method_add (w, "recenter 2");
 20133 #endif
 20134           goto recenter;
 20135         }
 20136 
 20137 #ifdef GLYPH_DEBUG
 20138       debug_method_add (w, "same window start");
 20139 #endif
 20140 
 20141       /* Try to redisplay starting at same place as before.
 20142          If point has not moved off frame, accept the results.  */
 20143       if (!current_matrix_up_to_date_p
 20144           /* Don't use try_window_reusing_current_matrix in this case
 20145              because a window scroll function can have changed the
 20146              buffer.  */
 20147           || !NILP (Vwindow_scroll_functions)
 20148           || MINI_WINDOW_P (w)
 20149           || !(used_current_matrix_p
 20150                = try_window_reusing_current_matrix (w)))
 20151         {
 20152           IF_DEBUG (debug_method_add (w, "1"));
 20153           clear_glyph_matrix (w->desired_matrix);
 20154           if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
 20155             /* -1 means we need to scroll.
 20156                0 means we need new matrices, but fonts_changed
 20157                is set in that case, so we will detect it below.  */
 20158             goto try_to_scroll;
 20159         }
 20160 
 20161       if (f->fonts_changed)
 20162         goto need_larger_matrices;
 20163 
 20164       if (w->cursor.vpos >= 0)
 20165         {
 20166           if (!just_this_one_p
 20167               || current_buffer->clip_changed
 20168               || BEG_UNCHANGED < CHARPOS (startp))
 20169             /* Forget any recorded base line for line number display.  */
 20170             w->base_line_number = 0;
 20171 
 20172           if (!cursor_row_fully_visible_p (w, true, false, false))
 20173             {
 20174               clear_glyph_matrix (w->desired_matrix);
 20175               last_line_misfit = true;
 20176             }
 20177             /* Drop through and scroll.  */
 20178           else
 20179             goto done;
 20180         }
 20181       else
 20182         clear_glyph_matrix (w->desired_matrix);
 20183     }
 20184 
 20185  try_to_scroll:
 20186 
 20187   /* Redisplay the mode line.  Select the buffer properly for that.  */
 20188   if (!update_mode_line)
 20189     {
 20190       update_mode_line = true;
 20191       w->update_mode_line = true;
 20192     }
 20193 
 20194   /* Try to scroll by specified few lines.  */
 20195   if ((0 < scroll_conservatively
 20196        /* FIXME: the option is supposed to affect minibuffers, but we
 20197           test MINI_WINDOW_P, which can also catch uses of
 20198           mini-windows for displaying the echo area.  Do we need to
 20199           distinguish these two use cases?  */
 20200        || (scroll_minibuffer_conservatively && MINI_WINDOW_P (w))
 20201        || 0 < emacs_scroll_step
 20202        || temp_scroll_step
 20203        || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
 20204        || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
 20205       && CHARPOS (startp) >= BEGV
 20206       && CHARPOS (startp) <= ZV)
 20207     {
 20208       /* The function returns -1 if new fonts were loaded, 1 if
 20209          successful, 0 if not successful.  */
 20210       int ss = try_scrolling (window, just_this_one_p,
 20211                               ((scroll_minibuffer_conservatively
 20212                                 && MINI_WINDOW_P (w))
 20213                                ? SCROLL_LIMIT + 1
 20214                                : scroll_conservatively),
 20215                               emacs_scroll_step,
 20216                               temp_scroll_step, last_line_misfit);
 20217       switch (ss)
 20218         {
 20219         case SCROLLING_SUCCESS:
 20220           goto done;
 20221 
 20222         case SCROLLING_NEED_LARGER_MATRICES:
 20223           goto need_larger_matrices;
 20224 
 20225         case SCROLLING_FAILED:
 20226           break;
 20227 
 20228         default:
 20229           emacs_abort ();
 20230         }
 20231     }
 20232 
 20233   /* Finally, just choose a place to start which positions point
 20234      according to user preferences.  */
 20235 
 20236  recenter:
 20237 
 20238 #ifdef GLYPH_DEBUG
 20239   debug_method_add (w, "recenter");
 20240 #endif
 20241 
 20242   /* Forget any previously recorded base line for line number display.  */
 20243   if (!buffer_unchanged_p)
 20244     w->base_line_number = 0;
 20245 
 20246   /* Determine the window start relative to point.  */
 20247   init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20248   it.current_y = it.last_visible_y;
 20249   if (centering_position < 0)
 20250     {
 20251       ptrdiff_t margin_pos = CHARPOS (startp);
 20252       Lisp_Object aggressive;
 20253       bool scrolling_up;
 20254 
 20255       /* If there is a scroll margin at the top of the window, find
 20256          its character position.  */
 20257       if (margin
 20258           /* Cannot call start_display if startp is not in the
 20259              accessible region of the buffer.  This can happen when we
 20260              have just switched to a different buffer and/or changed
 20261              its restriction.  In that case, startp is initialized to
 20262              the character position 1 (BEGV) because we did not yet
 20263              have chance to display the buffer even once.  */
 20264           && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
 20265         {
 20266           struct it it1;
 20267           void *it1data = NULL;
 20268 
 20269           SAVE_IT (it1, it, it1data);
 20270           start_display (&it1, w, startp);
 20271           move_it_vertically (&it1, margin * frame_line_height);
 20272           margin_pos = IT_CHARPOS (it1);
 20273           RESTORE_IT (&it, &it, it1data);
 20274         }
 20275       scrolling_up = PT > margin_pos;
 20276       aggressive =
 20277         scrolling_up
 20278         ? BVAR (current_buffer, scroll_up_aggressively)
 20279         : BVAR (current_buffer, scroll_down_aggressively);
 20280 
 20281       if (!MINI_WINDOW_P (w)
 20282           && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
 20283         {
 20284           int pt_offset = 0;
 20285 
 20286           /* Setting scroll-conservatively overrides
 20287              scroll-*-aggressively.  */
 20288           if (!scroll_conservatively && NUMBERP (aggressive))
 20289             {
 20290               double float_amount = XFLOATINT (aggressive);
 20291 
 20292               pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
 20293               if (pt_offset == 0 && float_amount > 0)
 20294                 pt_offset = 1;
 20295               if (pt_offset && margin > 0)
 20296                 margin -= 1;
 20297             }
 20298           /* Compute how much to move the window start backward from
 20299              point so that point will be displayed where the user
 20300              wants it.  */
 20301           if (scrolling_up)
 20302             {
 20303               centering_position = it.last_visible_y;
 20304               if (pt_offset)
 20305                 centering_position -= pt_offset;
 20306               centering_position -=
 20307                 (frame_line_height * (1 + margin + last_line_misfit)
 20308                  + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 20309               /* Don't let point enter the scroll margin near top of
 20310                  the window.  */
 20311               if (centering_position < margin * frame_line_height)
 20312                 centering_position = margin * frame_line_height;
 20313             }
 20314           else
 20315             centering_position = margin * frame_line_height + pt_offset;
 20316         }
 20317       else
 20318         /* Set the window start half the height of the window backward
 20319            from point.  */
 20320         centering_position = window_box_height (w) / 2;
 20321     }
 20322   if (current_buffer->long_line_optimizations_p
 20323       && it.line_wrap == TRUNCATE)
 20324     {
 20325       /* For very long and truncated lines, go back using a simplified
 20326          method, which ignored any inaccuracies due to line-height
 20327          differences, display properties/overlays, etc.  */
 20328       int nlines = centering_position / frame_line_height;
 20329 
 20330       while (nlines-- && IT_CHARPOS (it) > BEGV)
 20331         back_to_previous_visible_line_start (&it);
 20332       reseat_1 (&it, it.current.pos, true);
 20333     }
 20334   else
 20335     move_it_vertically_backward (&it, centering_position);
 20336 
 20337   eassert (IT_CHARPOS (it) >= BEGV);
 20338 
 20339   /* The function move_it_vertically_backward may move over more
 20340      than the specified y-distance.  If it->w is small, e.g. a
 20341      mini-buffer window, we may end up in front of the window's
 20342      display area.  Start displaying at the start of the line
 20343      containing PT in this case.  */
 20344   if (it.current_y <= 0)
 20345     {
 20346       init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
 20347       move_it_vertically_backward (&it, 0);
 20348       it.current_y = 0;
 20349     }
 20350 
 20351   it.current_x = it.hpos = 0;
 20352 
 20353   /* Set the window start position here explicitly, to avoid an
 20354      infinite loop in case the functions in window-scroll-functions
 20355      get errors.  */
 20356   set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
 20357 
 20358   /* Run scroll hooks.  */
 20359   startp = run_window_scroll_functions (window, it.current.pos);
 20360 
 20361   /* We invoke try_window and try_window_reusing_current_matrix below,
 20362      and they manipulate the bidi cache.  Save and restore the cache
 20363      state of our iterator, so we could continue using it after that.  */
 20364   itdata = bidi_shelve_cache ();
 20365 
 20366   /* Redisplay the window.  */
 20367   use_desired_matrix = false;
 20368   if (!current_matrix_up_to_date_p
 20369       || windows_or_buffers_changed
 20370       || f->cursor_type_changed
 20371       /* Don't use try_window_reusing_current_matrix in this case
 20372          because it can have changed the buffer.  */
 20373       || !NILP (Vwindow_scroll_functions)
 20374       || !just_this_one_p
 20375       || MINI_WINDOW_P (w)
 20376       || !(used_current_matrix_p
 20377            = try_window_reusing_current_matrix (w)))
 20378     use_desired_matrix = (try_window (window, startp, 0) == 1);
 20379 
 20380   bidi_unshelve_cache (itdata, false);
 20381 
 20382   /* If new fonts have been loaded (due to fontsets), give up.  We
 20383      have to start a new redisplay since we need to re-adjust glyph
 20384      matrices.  */
 20385   if (f->fonts_changed)
 20386     goto need_larger_matrices;
 20387 
 20388   /* If cursor did not appear assume that the middle of the window is
 20389      in the first line of the window.  Do it again with the next line.
 20390      (Imagine a window of height 100, displaying two lines of height
 20391      60.  Moving back 50 from it->last_visible_y will end in the first
 20392      line.)  */
 20393   if (w->cursor.vpos < 0)
 20394     {
 20395       if (w->window_end_valid && PT >= Z - w->window_end_pos)
 20396         {
 20397           clear_glyph_matrix (w->desired_matrix);
 20398           move_it_by_lines (&it, 1);
 20399           try_window (window, it.current.pos, 0);
 20400         }
 20401       else if (PT < IT_CHARPOS (it))
 20402         {
 20403           clear_glyph_matrix (w->desired_matrix);
 20404           move_it_by_lines (&it, -1);
 20405           try_window (window, it.current.pos, 0);
 20406         }
 20407       else if (scroll_conservatively > SCROLL_LIMIT
 20408                && (it.method == GET_FROM_STRING
 20409                    || overlay_touches_p (IT_CHARPOS (it)))
 20410                && IT_CHARPOS (it) < ZV)
 20411         {
 20412           /* If the window starts with a before-string that spans more
 20413              than one screen line, using that position to display the
 20414              window might fail to bring point into the view, because
 20415              start_display will always start by displaying the string,
 20416              whereas the code above determines where to set w->start
 20417              by the buffer position of the place where it takes screen
 20418              coordinates.  Try to recover by finding the next screen
 20419              line that displays buffer text.  */
 20420           ptrdiff_t pos0 = IT_CHARPOS (it);
 20421 
 20422           clear_glyph_matrix (w->desired_matrix);
 20423           do {
 20424             move_it_by_lines (&it, 1);
 20425           } while (IT_CHARPOS (it) == pos0);
 20426           try_window (window, it.current.pos, 0);
 20427         }
 20428       else
 20429         {
 20430           /* Not much we can do about it.  */
 20431         }
 20432     }
 20433 
 20434   /* Consider the following case: Window starts at BEGV, there is
 20435      invisible, intangible text at BEGV, so that display starts at
 20436      some point START > BEGV.  It can happen that we are called with
 20437      PT somewhere between BEGV and START.  Try to handle that case,
 20438      and similar ones.  */
 20439   if (w->cursor.vpos < 0)
 20440     {
 20441       /* Prefer the desired matrix to the current matrix, if possible,
 20442          in the fallback calculations below.  This is because using
 20443          the current matrix might completely goof, e.g. if its first
 20444          row is after point.  */
 20445       struct glyph_matrix *matrix =
 20446         use_desired_matrix ? w->desired_matrix : w->current_matrix;
 20447       /* First, try locating the proper glyph row for PT.  */
 20448       struct glyph_row *row =
 20449         row_containing_pos (w, PT, matrix->rows, NULL, 0);
 20450 
 20451       /* Sometimes point is at the beginning of invisible text that is
 20452          before the 1st character displayed in the row.  In that case,
 20453          row_containing_pos fails to find the row, because no glyphs
 20454          with appropriate buffer positions are present in the row.
 20455          Therefore, we next try to find the row which shows the 1st
 20456          position after the invisible text.  */
 20457       if (!row)
 20458         {
 20459           Lisp_Object val =
 20460             get_char_property_and_overlay (make_fixnum (PT), Qinvisible,
 20461                                            Qnil, NULL);
 20462 
 20463           if (TEXT_PROP_MEANS_INVISIBLE (val) != 0)
 20464             {
 20465               ptrdiff_t alt_pos;
 20466               Lisp_Object invis_end =
 20467                 Fnext_single_char_property_change (make_fixnum (PT), Qinvisible,
 20468                                                    Qnil, Qnil);
 20469 
 20470               if (FIXNATP (invis_end))
 20471                 alt_pos = XFIXNAT (invis_end);
 20472               else
 20473                 alt_pos = ZV;
 20474               row = row_containing_pos (w, alt_pos, matrix->rows, NULL, 0);
 20475             }
 20476         }
 20477       /* Finally, fall back on the first row of the window after the
 20478          tab-line and header line (if any).  This is slightly better
 20479          than not displaying the cursor at all.  */
 20480       if (!row)
 20481         {
 20482           row = matrix->rows;
 20483           /* Skip the tab-line and header-line rows, if any.  */
 20484           if (row->tab_line_p)
 20485             ++row;
 20486           if (row->mode_line_p)
 20487             ++row;
 20488         }
 20489       set_cursor_from_row (w, row, matrix, 0, 0, 0, 0);
 20490     }
 20491 
 20492   if (!cursor_row_fully_visible_p (w, false, false, false))
 20493     {
 20494       /* If vscroll is enabled, disable it and try again.  */
 20495       if (w->vscroll)
 20496         {
 20497           w->vscroll = 0;
 20498           clear_glyph_matrix (w->desired_matrix);
 20499           goto recenter;
 20500         }
 20501 
 20502       /* Users who set scroll-conservatively to a large number want
 20503          point just above/below the scroll margin.  If we ended up
 20504          with point's row partially visible, move the window start to
 20505          make that row fully visible and out of the margin.  */
 20506       if (scroll_conservatively > SCROLL_LIMIT)
 20507         {
 20508           int window_total_lines
 20509             = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
 20510           bool move_down = w->cursor.vpos >= window_total_lines / 2;
 20511 
 20512           move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
 20513           clear_glyph_matrix (w->desired_matrix);
 20514           if (1 == try_window (window, it.current.pos,
 20515                                TRY_WINDOW_CHECK_MARGINS))
 20516             goto done;
 20517         }
 20518 
 20519       /* If centering point failed to make the whole line visible,
 20520          put point at the top instead.  That has to make the whole line
 20521          visible, if it can be done.  */
 20522       if (centering_position == 0)
 20523         goto done;
 20524 
 20525       clear_glyph_matrix (w->desired_matrix);
 20526       centering_position = 0;
 20527       goto recenter;
 20528     }
 20529 
 20530  done:
 20531 
 20532   SET_TEXT_POS_FROM_MARKER (startp, w->start);
 20533   w->start_at_line_beg = (CHARPOS (startp) == BEGV
 20534                           || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
 20535 
 20536   /* Display the mode line, header line, and tab-line, if we must.  */
 20537   if ((update_mode_line
 20538        /* If window not full width, must redo its mode line
 20539           if (a) the window to its side is being redone and
 20540           (b) we do a frame-based redisplay.  This is a consequence
 20541           of how inverted lines are drawn in frame-based redisplay.  */
 20542        || (!just_this_one_p
 20543            && !FRAME_WINDOW_P (f)
 20544            && !WINDOW_FULL_WIDTH_P (w))
 20545        /* Line number to display.  */
 20546        || w->base_line_pos > 0
 20547        /* Column number is displayed and different from the one displayed.  */
 20548        || (w->column_number_displayed != -1
 20549            && (w->column_number_displayed != current_column ())))
 20550       /* This means that the window has a mode line.  */
 20551       && (window_wants_mode_line (w)
 20552           || window_wants_header_line (w)
 20553           || window_wants_tab_line (w)))
 20554     {
 20555       specpdl_ref count1 = SPECPDL_INDEX ();
 20556 
 20557       specbind (Qinhibit_quit, Qt);
 20558       display_mode_lines (w);
 20559       unbind_to (count1, Qnil);
 20560 
 20561       /* If mode line height has changed, arrange for a thorough
 20562          immediate redisplay using the correct mode line height.  */
 20563       if (window_wants_mode_line (w)
 20564           && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
 20565         {
 20566           f->fonts_changed = true;
 20567           w->mode_line_height = -1;
 20568           MATRIX_MODE_LINE_ROW (w->current_matrix)->height
 20569             = DESIRED_MODE_LINE_HEIGHT (w);
 20570         }
 20571 
 20572       /* If tab line height has changed, arrange for a thorough
 20573          immediate redisplay using the correct tab line height.  */
 20574       if (window_wants_tab_line (w)
 20575           && CURRENT_TAB_LINE_HEIGHT (w) != DESIRED_TAB_LINE_HEIGHT (w))
 20576         {
 20577           f->fonts_changed = true;
 20578           w->tab_line_height = -1;
 20579           MATRIX_TAB_LINE_ROW (w->current_matrix)->height
 20580             = DESIRED_TAB_LINE_HEIGHT (w);
 20581         }
 20582 
 20583       /* If header line height has changed, arrange for a thorough
 20584          immediate redisplay using the correct header line height.  */
 20585       if (window_wants_header_line (w)
 20586           && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
 20587         {
 20588           f->fonts_changed = true;
 20589           w->header_line_height = -1;
 20590           MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
 20591             = DESIRED_HEADER_LINE_HEIGHT (w);
 20592         }
 20593 
 20594       if (f->fonts_changed)
 20595         goto need_larger_matrices;
 20596     }
 20597 
 20598   if (!line_number_displayed && w->base_line_pos != -1)
 20599     {
 20600       w->base_line_pos = 0;
 20601       w->base_line_number = 0;
 20602     }
 20603 
 20604  finish_menu_bars:
 20605 
 20606   /* When we reach a frame's selected window, redo the frame's menu
 20607      bar, tool bar, tab-bar, and the frame's title.  */
 20608   if (update_mode_line
 20609       && EQ (FRAME_SELECTED_WINDOW (f), window))
 20610     {
 20611       bool redisplay_menu_p;
 20612 
 20613       if (FRAME_WINDOW_P (f))
 20614         {
 20615 #ifdef HAVE_EXT_MENU_BAR
 20616           redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
 20617 #else
 20618           redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20619 #endif
 20620         }
 20621       else
 20622         redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
 20623 
 20624       if (redisplay_menu_p)
 20625         display_menu_bar (w);
 20626 
 20627 #ifdef HAVE_WINDOW_SYSTEM
 20628       if (FRAME_WINDOW_P (f))
 20629         {
 20630           if (WINDOWP (f->tab_bar_window)
 20631               && (FRAME_TAB_BAR_LINES (f) > 0
 20632                   || !NILP (Vauto_resize_tab_bars))
 20633               && redisplay_tab_bar (f))
 20634             ignore_mouse_drag_p = true;
 20635 
 20636 #ifdef HAVE_EXT_TOOL_BAR
 20637           if (FRAME_EXTERNAL_TOOL_BAR (f))
 20638             update_frame_tool_bar (f);
 20639 #else
 20640           if (WINDOWP (f->tool_bar_window)
 20641               && (FRAME_TOOL_BAR_LINES (f) > 0
 20642                   || !NILP (Vauto_resize_tool_bars))
 20643               && redisplay_tool_bar (f))
 20644             ignore_mouse_drag_p = true;
 20645 #endif
 20646         }
 20647       else
 20648         {
 20649           if ((FRAME_TAB_BAR_LINES (f) > 0))
 20650             display_tab_bar (w);
 20651         }
 20652 
 20653       gui_consider_frame_title (w->frame);
 20654 #else
 20655       if ((FRAME_TAB_BAR_LINES (f) > 0))
 20656         display_tab_bar (w);
 20657 #endif
 20658     }
 20659 
 20660 #ifdef HAVE_WINDOW_SYSTEM
 20661   if (FRAME_WINDOW_P (f)
 20662       && update_window_fringes (w, (just_this_one_p
 20663                                     || (!used_current_matrix_p && !overlay_arrow_seen)
 20664                                     || w->pseudo_window_p)))
 20665     {
 20666       update_begin (f);
 20667       block_input ();
 20668       if (draw_window_fringes (w, true))
 20669         {
 20670           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 20671             gui_draw_right_divider (w);
 20672           else
 20673             gui_draw_vertical_border (w);
 20674         }
 20675       unblock_input ();
 20676       update_end (f);
 20677     }
 20678 
 20679   if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 20680     gui_draw_bottom_divider (w);
 20681 #endif /* HAVE_WINDOW_SYSTEM */
 20682 
 20683   /* We go to this label, with fonts_changed set, if it is
 20684      necessary to try again using larger glyph matrices.
 20685      We have to redeem the scroll bar even in this case,
 20686      because the loop in redisplay_internal expects that.  */
 20687  need_larger_matrices:
 20688   ;
 20689  finish_scroll_bars:
 20690 
 20691    if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w) || WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20692     {
 20693       if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
 20694         /* Set the thumb's position and size.  */
 20695         set_vertical_scroll_bar (w);
 20696 
 20697       if (WINDOW_HAS_HORIZONTAL_SCROLL_BAR (w))
 20698         /* Set the thumb's position and size.  */
 20699         set_horizontal_scroll_bar (w);
 20700 
 20701       /* Note that we actually used the scroll bar attached to this
 20702          window, so it shouldn't be deleted at the end of redisplay.  */
 20703       if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
 20704         (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
 20705     }
 20706 
 20707   /* Restore current_buffer and value of point in it.  The window
 20708      update may have changed the buffer, so first make sure `opoint'
 20709      is still valid (Bug#6177).  */
 20710   if (CHARPOS (opoint) < BEGV)
 20711     TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
 20712   else if (CHARPOS (opoint) > ZV)
 20713     TEMP_SET_PT_BOTH (Z, Z_BYTE);
 20714   else
 20715     TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
 20716 
 20717   set_buffer_internal_1 (old);
 20718   /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
 20719      shorter.  This can be caused by log truncation in *Messages*.  */
 20720   if (CHARPOS (lpoint) <= ZV)
 20721     TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 20722 
 20723   unbind_to (count, Qnil);
 20724 }
 20725 
 20726 
 20727 /* Build the complete desired matrix of WINDOW with a window start
 20728    buffer position POS.
 20729 
 20730    Value is 1 if successful.  It is zero if fonts were loaded during
 20731    redisplay or the dimensions of the desired matrix were found
 20732    insufficient, which makes re-adjusting glyph matrices necessary.
 20733    Value is -1 if point would appear in the scroll margins.  (We check
 20734    the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is unset in
 20735    FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is set in
 20736    FLAGS.)
 20737 
 20738    Note that 'x-show-tip' invokes this function in a special way, and
 20739    in that case the return value of zero doesn't necessarily mean the
 20740    glyph matrices need to be re-adjusted, if the entire text of the
 20741    tooltip was processed and has its glyphs in the matrix's glyph
 20742    rows, i.e. if the dimensions of the matrix were found insufficient
 20743    while producing empty glyph rows beyond ZV.  */
 20744 
 20745 int
 20746 try_window (Lisp_Object window, struct text_pos pos, int flags)
 20747 {
 20748   struct window *w = XWINDOW (window);
 20749   struct it it;
 20750   struct glyph_row *last_text_row = NULL;
 20751   struct frame *f = XFRAME (w->frame);
 20752   int cursor_vpos = w->cursor.vpos;
 20753 
 20754   /* Make POS the new window start.  */
 20755   set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
 20756 
 20757   /* Mark cursor position as unknown.  No overlay arrow seen.  */
 20758   w->cursor.vpos = -1;
 20759   overlay_arrow_seen = false;
 20760 
 20761   /* Initialize iterator and info to start at POS.  */
 20762   start_display (&it, w, pos);
 20763   it.glyph_row->reversed_p = false;
 20764 
 20765   /* Display all lines of W.  */
 20766   while (it.current_y < it.last_visible_y)
 20767     {
 20768       int last_row_scale = it.w->nrows_scale_factor;
 20769       int last_col_scale = it.w->ncols_scale_factor;
 20770       if (display_line (&it, cursor_vpos))
 20771         last_text_row = it.glyph_row - 1;
 20772       if (f->fonts_changed
 20773           && !((flags & TRY_WINDOW_IGNORE_FONTS_CHANGE)
 20774                /* If the matrix dimensions are insufficient, we _must_
 20775                   fail and let dispnew.c reallocate the matrix.  */
 20776                && last_row_scale == it.w->nrows_scale_factor
 20777                && last_col_scale == it.w->ncols_scale_factor))
 20778         return 0;
 20779     }
 20780 
 20781   /* Save the character position of 'it' before we call
 20782      'start_display' again.  */
 20783   ptrdiff_t it_charpos = IT_CHARPOS (it);
 20784 
 20785   /* Don't let the cursor end in the scroll margins.  However, when
 20786      the window is vscrolled, we leave it to vscroll to handle the
 20787      margins, see window_scroll_pixel_based.  */
 20788   if ((flags & TRY_WINDOW_CHECK_MARGINS)
 20789       && w->vscroll == 0
 20790       && !MINI_WINDOW_P (w))
 20791     {
 20792       int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 20793       int bot_scroll_margin = top_scroll_margin;
 20794       if (window_wants_header_line (w))
 20795         top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 20796       if (window_wants_tab_line (w))
 20797         top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 20798       start_display (&it, w, pos);
 20799 
 20800       if ((w->cursor.y >= 0
 20801            && w->cursor.y < top_scroll_margin
 20802            && CHARPOS (pos) > BEGV)
 20803           /* rms: considering make_cursor_line_fully_visible_p here
 20804              seems to give wrong results.  We don't want to recenter
 20805              when the last line is partly visible, we want to allow
 20806              that case to be handled in the usual way.  */
 20807           || w->cursor.y > (it.last_visible_y - partial_line_height (&it)
 20808                             - bot_scroll_margin - 1))
 20809         {
 20810           w->cursor.vpos = -1;
 20811           clear_glyph_matrix (w->desired_matrix);
 20812           return -1;
 20813         }
 20814     }
 20815 
 20816   /* If bottom moved off end of frame, change mode line percentage.  */
 20817   if (w->window_end_pos <= 0 && Z != it_charpos)
 20818     w->update_mode_line = true;
 20819 
 20820   /* Set window_end_pos to the offset of the last character displayed
 20821      on the window from the end of current_buffer.  Set
 20822      window_end_vpos to its row number.  */
 20823   if (last_text_row)
 20824     {
 20825       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
 20826       adjust_window_ends (w, last_text_row, false);
 20827       eassert
 20828         (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
 20829                                                  w->window_end_vpos)));
 20830     }
 20831   else
 20832     {
 20833       w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 20834       w->window_end_pos = Z - ZV;
 20835       w->window_end_vpos = 0;
 20836     }
 20837 
 20838   /* But that is not valid info until redisplay finishes.  */
 20839   w->window_end_valid = false;
 20840   return 1;
 20841 }
 20842 
 20843 
 20844 
 20845 /************************************************************************
 20846     Window redisplay reusing current matrix when buffer has not changed
 20847  ************************************************************************/
 20848 
 20849 /* Try redisplay of window W showing an unchanged buffer with a
 20850    different window start than the last time it was displayed by
 20851    reusing its current matrix.  Value is true if successful.
 20852    W->start is the new window start.  */
 20853 
 20854 static bool
 20855 try_window_reusing_current_matrix (struct window *w)
 20856 {
 20857   struct frame *f = XFRAME (w->frame);
 20858   struct glyph_row *bottom_row;
 20859   struct it it;
 20860   struct run run;
 20861   struct text_pos start, new_start;
 20862   int nrows_scrolled, i;
 20863   struct glyph_row *last_text_row;
 20864   struct glyph_row *last_reused_text_row;
 20865   struct glyph_row *start_row;
 20866   int start_vpos, min_y, max_y;
 20867 
 20868 #ifdef GLYPH_DEBUG
 20869   if (inhibit_try_window_reusing)
 20870     return false;
 20871 #endif
 20872 
 20873   if (/* This function doesn't handle terminal frames.  */
 20874       !FRAME_WINDOW_P (f)
 20875       /* Don't try to reuse the display if windows have been split
 20876          or such.  */
 20877       || windows_or_buffers_changed
 20878       || f->cursor_type_changed
 20879       /* This function cannot handle buffers where the overlay arrow
 20880          is shown on the fringes, because if the arrow position
 20881          changes, we cannot just reuse the current matrix.  */
 20882       || overlay_arrow_in_current_buffer_p ())
 20883     return false;
 20884 
 20885   /* Can't do this if showing trailing whitespace.  */
 20886   if (!NILP (Vshow_trailing_whitespace))
 20887     return false;
 20888 
 20889   /* If top-line visibility has changed, give up.  */
 20890   if (window_wants_tab_line (w)
 20891       != MATRIX_TAB_LINE_ROW (w->current_matrix)->mode_line_p)
 20892     return false;
 20893 
 20894   /* If top-line visibility has changed, give up.  */
 20895   if (window_wants_header_line (w)
 20896       != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
 20897     return false;
 20898 
 20899   /* Give up if old or new display is scrolled vertically.  We could
 20900      make this function handle this, but right now it doesn't.  */
 20901   start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 20902   if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
 20903     return false;
 20904 
 20905   /* Clear the desired matrix for the display below.  */
 20906   clear_glyph_matrix (w->desired_matrix);
 20907 
 20908   /* Give up if line numbers are being displayed, because reusing the
 20909      current matrix might use the wrong width for line-number
 20910      display.  */
 20911   if (!NILP (Vdisplay_line_numbers))
 20912     return false;
 20913 
 20914   /* Can't scroll the display of w32 GUI frames when position of point
 20915      is indicated by the system caret, because scrolling the display
 20916      will then "copy" the pixels used by the caret.  */
 20917 #ifdef HAVE_NTGUI
 20918   if (w32_use_visible_system_caret)
 20919     return false;
 20920 #endif
 20921 
 20922   /* The variable new_start now holds the new window start.  The old
 20923      start `start' can be determined from the current matrix.  */
 20924   SET_TEXT_POS_FROM_MARKER (new_start, w->start);
 20925   start = start_row->minpos;
 20926   start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20927 
 20928   if (CHARPOS (new_start) <= CHARPOS (start))
 20929     {
 20930       /* Don't use this method if the display starts with an ellipsis
 20931          displayed for invisible text.  It's not easy to handle that case
 20932          below, and it's certainly not worth the effort since this is
 20933          not a frequent case.  */
 20934       if (in_ellipses_for_invisible_text_p (&start_row->start, w))
 20935         return false;
 20936 
 20937       IF_DEBUG (debug_method_add (w, "twu1"));
 20938 
 20939       /* Display up to a row that can be reused.  The variable
 20940          last_text_row is set to the last row displayed that displays
 20941          text.  Note that it.vpos == 0 if or if not there is a
 20942          header-line; it's not the same as the MATRIX_ROW_VPOS!  */
 20943       start_display (&it, w, new_start);
 20944       w->cursor.vpos = -1;
 20945       last_text_row = last_reused_text_row = NULL;
 20946 
 20947       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 20948         {
 20949           /* If we have reached into the characters in the START row,
 20950              that means the line boundaries have changed.  So we
 20951              can't start copying with the row START.  Maybe it will
 20952              work to start copying with the following row.  */
 20953           while (IT_CHARPOS (it) > CHARPOS (start))
 20954             {
 20955               /* Advance to the next row as the "start".  */
 20956               start_row++;
 20957               start = start_row->minpos;
 20958               /* If there are no more rows to try, or just one, give up.  */
 20959               if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
 20960                   || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
 20961                   || CHARPOS (start) == ZV)
 20962                 {
 20963                   clear_glyph_matrix (w->desired_matrix);
 20964                   return false;
 20965                 }
 20966 
 20967               start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
 20968             }
 20969           /* If we have reached alignment, we can copy the rest of the
 20970              rows.  */
 20971           if (IT_CHARPOS (it) == CHARPOS (start)
 20972               /* Don't accept "alignment" inside a display vector,
 20973                  since start_row could have started in the middle of
 20974                  that same display vector (thus their character
 20975                  positions match), and we have no way of telling if
 20976                  that is the case.  */
 20977               && it.current.dpvec_index < 0)
 20978             break;
 20979 
 20980           it.glyph_row->reversed_p = false;
 20981           if (display_line (&it, -1))
 20982             last_text_row = it.glyph_row - 1;
 20983 
 20984         }
 20985 
 20986       /* A value of current_y < last_visible_y means that we stopped
 20987          at the previous window start, which in turn means that we
 20988          have at least one reusable row.  */
 20989       if (it.current_y < it.last_visible_y)
 20990         {
 20991           struct glyph_row *row;
 20992 
 20993           /* IT.vpos always starts from 0; it counts text lines.  */
 20994           nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
 20995 
 20996           /* Find PT if not already found in the lines displayed.  */
 20997           if (w->cursor.vpos < 0)
 20998             {
 20999               int dy = it.current_y - start_row->y;
 21000 
 21001               row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21002               row = row_containing_pos (w, PT, row, NULL, dy);
 21003               if (row)
 21004                 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
 21005                                      dy, nrows_scrolled);
 21006               else
 21007                 {
 21008                   clear_glyph_matrix (w->desired_matrix);
 21009                   return false;
 21010                 }
 21011             }
 21012 
 21013           /* Scroll the display.  Do it before the current matrix is
 21014              changed.  The problem here is that update has not yet
 21015              run, i.e. part of the current matrix is not up to date.
 21016              scroll_run_hook will clear the cursor, and use the
 21017              current matrix to get the height of the row the cursor is
 21018              in.  */
 21019           run.current_y = start_row->y;
 21020           run.desired_y = it.current_y;
 21021           run.height = it.last_visible_y - it.current_y;
 21022 
 21023           if (run.height > 0 && run.current_y != run.desired_y)
 21024             {
 21025 #ifdef HAVE_WINDOW_SYSTEM
 21026               update_begin (f);
 21027               gui_update_window_begin (w);
 21028               FRAME_RIF (f)->clear_window_mouse_face (w);
 21029               FRAME_RIF (f)->scroll_run_hook (w, &run);
 21030               gui_update_window_end (w, false, false);
 21031               update_end (f);
 21032 #endif
 21033             }
 21034 
 21035           /* Shift current matrix down by nrows_scrolled lines.  */
 21036           bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21037           rotate_matrix (w->current_matrix,
 21038                          start_vpos,
 21039                          MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21040                          nrows_scrolled);
 21041 
 21042           /* Disable lines that must be updated.  */
 21043           for (i = 0; i < nrows_scrolled; ++i)
 21044             (start_row + i)->enabled_p = false;
 21045 
 21046           /* Re-compute Y positions.  */
 21047           min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21048           max_y = it.last_visible_y;
 21049           for (row = start_row + nrows_scrolled;
 21050                row < bottom_row;
 21051                ++row)
 21052             {
 21053               row->y = it.current_y;
 21054               row->visible_height = row->height;
 21055 
 21056               if (row->y < min_y)
 21057                 row->visible_height -= min_y - row->y;
 21058               if (row->y + row->height > max_y)
 21059                 row->visible_height -= row->y + row->height - max_y;
 21060               if (row->fringe_bitmap_periodic_p)
 21061                 row->redraw_fringe_bitmaps_p = true;
 21062 
 21063               it.current_y += row->height;
 21064 
 21065               if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21066                 last_reused_text_row = row;
 21067               if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
 21068                 break;
 21069             }
 21070 
 21071           /* Disable lines in the current matrix which are now
 21072              below the window.  */
 21073           for (++row; row < bottom_row; ++row)
 21074             row->enabled_p = row->mode_line_p = row->tab_line_p = false;
 21075         }
 21076 
 21077       /* Update window_end_pos etc.; last_reused_text_row is the last
 21078          reused row from the current matrix containing text, if any.
 21079          The value of last_text_row is the last displayed line
 21080          containing text.  */
 21081       if (last_reused_text_row)
 21082         adjust_window_ends (w, last_reused_text_row, true);
 21083       else if (last_text_row)
 21084         adjust_window_ends (w, last_text_row, false);
 21085       else
 21086         {
 21087           /* This window must be completely empty.  */
 21088           w->window_end_bytepos = Z_BYTE - ZV_BYTE;
 21089           w->window_end_pos = Z - ZV;
 21090           w->window_end_vpos = 0;
 21091         }
 21092       w->window_end_valid = false;
 21093 
 21094       /* Update hint: don't try scrolling again in update_window.  */
 21095       w->desired_matrix->no_scrolling_p = true;
 21096 
 21097 #ifdef GLYPH_DEBUG
 21098       debug_method_add (w, "try_window_reusing_current_matrix 1");
 21099 #endif
 21100       return true;
 21101     }
 21102   else if (CHARPOS (new_start) > CHARPOS (start))
 21103     {
 21104       struct glyph_row *pt_row, *row;
 21105       struct glyph_row *first_reusable_row;
 21106       struct glyph_row *first_row_to_display;
 21107       int dy;
 21108       int yb = window_text_bottom_y (w);
 21109 
 21110       /* Find the row starting at new_start, if there is one.  Don't
 21111          reuse a partially visible line at the end.  */
 21112       first_reusable_row = start_row;
 21113       while (first_reusable_row->enabled_p
 21114              && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
 21115              && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21116                  < CHARPOS (new_start)))
 21117         ++first_reusable_row;
 21118 
 21119       /* Give up if there is no row to reuse.  */
 21120       if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
 21121           || !first_reusable_row->enabled_p
 21122           || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
 21123               != CHARPOS (new_start)))
 21124         return false;
 21125 
 21126       /* We can reuse fully visible rows beginning with
 21127          first_reusable_row to the end of the window.  Set
 21128          first_row_to_display to the first row that cannot be reused.
 21129          Set pt_row to the row containing point, if there is any.  */
 21130       pt_row = NULL;
 21131       for (first_row_to_display = first_reusable_row;
 21132            MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
 21133            ++first_row_to_display)
 21134         {
 21135           if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
 21136               && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21137                   || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
 21138                       && first_row_to_display->ends_at_zv_p
 21139                       && pt_row == NULL)))
 21140             pt_row = first_row_to_display;
 21141         }
 21142 
 21143       if (first_row_to_display->y >= yb)
 21144         return false;
 21145 
 21146       /* Start displaying at the start of first_row_to_display.  */
 21147       init_to_row_start (&it, w, first_row_to_display);
 21148 
 21149       nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
 21150                         - start_vpos);
 21151       it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
 21152                  - nrows_scrolled);
 21153       it.current_y = (first_row_to_display->y - first_reusable_row->y
 21154                       + WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w));
 21155 
 21156       /* Display lines beginning with first_row_to_display in the
 21157          desired matrix.  Set last_text_row to the last row displayed
 21158          that displays text.  */
 21159       it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
 21160       if (pt_row == NULL)
 21161         w->cursor.vpos = -1;
 21162       last_text_row = NULL;
 21163       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 21164         if (display_line (&it, w->cursor.vpos))
 21165           last_text_row = it.glyph_row - 1;
 21166 
 21167       /* If point is in a reused row, adjust y and vpos of the cursor
 21168          position.  */
 21169       if (pt_row)
 21170         {
 21171           w->cursor.vpos -= nrows_scrolled;
 21172           w->cursor.y -= first_reusable_row->y - start_row->y;
 21173         }
 21174 
 21175       /* Give up if point isn't in a row displayed or reused.  (This
 21176          also handles the case where w->cursor.vpos < nrows_scrolled
 21177          after the calls to display_line, which can happen with scroll
 21178          margins.  See bug#1295.)  */
 21179       if (w->cursor.vpos < 0)
 21180         {
 21181           clear_glyph_matrix (w->desired_matrix);
 21182           return false;
 21183         }
 21184 
 21185       /* Scroll the display.  */
 21186       run.current_y = first_reusable_row->y;
 21187       run.desired_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21188       run.height = it.last_visible_y - run.current_y;
 21189       dy = run.current_y - run.desired_y;
 21190 
 21191       if (run.height)
 21192         {
 21193 #ifdef HAVE_WINDOW_SYSTEM
 21194           update_begin (f);
 21195           gui_update_window_begin (w);
 21196           FRAME_RIF (f)->clear_window_mouse_face (w);
 21197           FRAME_RIF (f)->scroll_run_hook (w, &run);
 21198           gui_update_window_end (w, false, false);
 21199           update_end (f);
 21200 #endif
 21201         }
 21202 
 21203       /* Adjust Y positions of reused rows.  */
 21204       bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
 21205       min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 21206       max_y = it.last_visible_y;
 21207       for (row = first_reusable_row; row < first_row_to_display; ++row)
 21208         {
 21209           row->y -= dy;
 21210           row->visible_height = row->height;
 21211           if (row->y < min_y)
 21212             row->visible_height -= min_y - row->y;
 21213           if (row->y + row->height > max_y)
 21214             row->visible_height -= row->y + row->height - max_y;
 21215           if (row->fringe_bitmap_periodic_p)
 21216             row->redraw_fringe_bitmaps_p = true;
 21217         }
 21218 
 21219       /* Scroll the current matrix.  */
 21220       eassert (nrows_scrolled > 0);
 21221       rotate_matrix (w->current_matrix,
 21222                      start_vpos,
 21223                      MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
 21224                      -nrows_scrolled);
 21225 
 21226       /* Disable rows not reused.  */
 21227       for (row -= nrows_scrolled; row < bottom_row; ++row)
 21228         row->enabled_p = false;
 21229 
 21230       /* Point may have moved to a different line, so we cannot assume that
 21231          the previous cursor position is valid; locate the correct row.  */
 21232       if (pt_row)
 21233         {
 21234           for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
 21235                row < bottom_row
 21236                  && PT >= MATRIX_ROW_END_CHARPOS (row)
 21237                  && !row->ends_at_zv_p;
 21238                row++)
 21239             {
 21240               w->cursor.vpos++;
 21241               w->cursor.y = row->y;
 21242             }
 21243           if (row < bottom_row)
 21244             {
 21245               /* Can't simply scan the row for point with
 21246                  bidi-reordered glyph rows.  Let set_cursor_from_row
 21247                  figure out where to put the cursor, and if it fails,
 21248                  give up.  */
 21249               if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
 21250                 {
 21251                   if (!set_cursor_from_row (w, row, w->current_matrix,
 21252                                             0, 0, 0, 0))
 21253                     {
 21254                       clear_glyph_matrix (w->desired_matrix);
 21255                       return false;
 21256                     }
 21257                 }
 21258               else
 21259                 {
 21260                   struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
 21261                   struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21262 
 21263                   for (; glyph < end
 21264                          && (!BUFFERP (glyph->object)
 21265                              || glyph->charpos < PT);
 21266                        glyph++)
 21267                     {
 21268                       w->cursor.hpos++;
 21269                       w->cursor.x += glyph->pixel_width;
 21270                     }
 21271                 }
 21272             }
 21273         }
 21274 
 21275       /* Adjust window end.  A null value of last_text_row means that
 21276          the window end is in reused rows which in turn means that
 21277          only its vpos can have changed.  */
 21278       if (last_text_row)
 21279         adjust_window_ends (w, last_text_row, false);
 21280       else
 21281         w->window_end_vpos -= nrows_scrolled;
 21282 
 21283       w->window_end_valid = false;
 21284       w->desired_matrix->no_scrolling_p = true;
 21285 
 21286 #ifdef GLYPH_DEBUG
 21287       debug_method_add (w, "try_window_reusing_current_matrix 2");
 21288 #endif
 21289       return true;
 21290     }
 21291 
 21292   return false;
 21293 }
 21294 
 21295 
 21296 
 21297 /************************************************************************
 21298    Window redisplay reusing current matrix when buffer has changed
 21299  ************************************************************************/
 21300 
 21301 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
 21302 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
 21303                                                           ptrdiff_t *, ptrdiff_t *);
 21304 static struct glyph_row *
 21305 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
 21306                                struct glyph_row *);
 21307 
 21308 
 21309 /* Return the last row in MATRIX displaying text.  If row START is
 21310    non-null, start searching with that row.  IT gives the dimensions
 21311    of the display.  Value is null if matrix is empty; otherwise it is
 21312    a pointer to the row found.  */
 21313 
 21314 static struct glyph_row *
 21315 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
 21316                                struct glyph_row *start)
 21317 {
 21318   struct glyph_row *row, *row_found;
 21319 
 21320   /* Set row_found to the last row in IT->w's current matrix
 21321      displaying text.  The loop looks funny but think of partially
 21322      visible lines.  */
 21323   row_found = NULL;
 21324   row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
 21325   while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21326     {
 21327       eassert (row->enabled_p);
 21328       row_found = row;
 21329       if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
 21330         break;
 21331       ++row;
 21332     }
 21333 
 21334   return row_found;
 21335 }
 21336 
 21337 
 21338 /* Return the last row in the current matrix of W that is not affected
 21339    by changes at the start of current_buffer that occurred since W's
 21340    current matrix was built.  Value is null if no such row exists.
 21341 
 21342    BEG_UNCHANGED is the number of characters unchanged at the start of
 21343    current_buffer.  BEG + BEG_UNCHANGED is the buffer position of the
 21344    first changed character in current_buffer.  Characters at positions <
 21345    BEG + BEG_UNCHANGED are at the same buffer positions as they were
 21346    when the current matrix was built.  */
 21347 
 21348 static struct glyph_row *
 21349 find_last_unchanged_at_beg_row (struct window *w)
 21350 {
 21351   ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
 21352   struct glyph_row *row;
 21353   struct glyph_row *row_found = NULL;
 21354   int yb = window_text_bottom_y (w);
 21355 
 21356   /* Find the last row displaying unchanged text.  */
 21357   for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21358        MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21359          && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
 21360        ++row)
 21361     {
 21362       if (/* If row ends before first_changed_pos, it is unchanged,
 21363              except in some case.  */
 21364           MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
 21365           /* When row ends in ZV and we write at ZV it is not
 21366              unchanged.  */
 21367           && !row->ends_at_zv_p
 21368           /* When first_changed_pos is the end of a continued line,
 21369              row is not unchanged because it may be no longer
 21370              continued.  */
 21371           && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
 21372                && (row->continued_p
 21373                    || row->exact_window_width_line_p))
 21374           /* If ROW->end is beyond ZV, then ROW->end is outdated and
 21375              needs to be recomputed, so don't consider this row as
 21376              unchanged.  This happens when the last line was
 21377              bidi-reordered and was killed immediately before this
 21378              redisplay cycle.  In that case, ROW->end stores the
 21379              buffer position of the first visual-order character of
 21380              the killed text, which is now beyond ZV.  */
 21381           && CHARPOS (row->end.pos) <= ZV)
 21382         row_found = row;
 21383 
 21384       /* Stop if last visible row.  */
 21385       if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
 21386         break;
 21387     }
 21388 
 21389   return row_found;
 21390 }
 21391 
 21392 
 21393 /* Find the first glyph row in the current matrix of W that is not
 21394    affected by changes at the end of current_buffer since the
 21395    time W's current matrix was built.
 21396 
 21397    Return in *DELTA the number of chars by which buffer positions in
 21398    unchanged text at the end of current_buffer must be adjusted.
 21399 
 21400    Return in *DELTA_BYTES the corresponding number of bytes.
 21401 
 21402    Value is null if no such row exists, i.e. all rows are affected by
 21403    changes.  */
 21404 
 21405 static struct glyph_row *
 21406 find_first_unchanged_at_end_row (struct window *w,
 21407                                  ptrdiff_t *delta, ptrdiff_t *delta_bytes)
 21408 {
 21409   struct glyph_row *row;
 21410   struct glyph_row *row_found = NULL;
 21411 
 21412   *delta = *delta_bytes = 0;
 21413 
 21414   /* Display must not have been paused, otherwise the current matrix
 21415      is not up to date.  */
 21416   eassert (w->window_end_valid);
 21417 
 21418   /* A value of window_end_pos >= END_UNCHANGED means that the window
 21419      end is in the range of changed text.  If so, there is no
 21420      unchanged row at the end of W's current matrix.  */
 21421   if (w->window_end_pos >= END_UNCHANGED)
 21422     return NULL;
 21423 
 21424   /* Set row to the last row in W's current matrix displaying text.  */
 21425   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21426 
 21427   /* If matrix is entirely empty, no unchanged row exists.  */
 21428   if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21429     {
 21430       /* The value of row is the last glyph row in the matrix having a
 21431          meaningful buffer position in it.  The end position of row
 21432          corresponds to window_end_pos.  This allows us to translate
 21433          buffer positions in the current matrix to current buffer
 21434          positions for characters not in changed text.  */
 21435       ptrdiff_t Z_old =
 21436         MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21437       ptrdiff_t Z_BYTE_old =
 21438         MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21439       ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
 21440       struct glyph_row *first_text_row
 21441         = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 21442 
 21443       *delta = Z - Z_old;
 21444       *delta_bytes = Z_BYTE - Z_BYTE_old;
 21445 
 21446       /* Set last_unchanged_pos to the buffer position of the last
 21447          character in the buffer that has not been changed.  Z is the
 21448          index + 1 of the last character in current_buffer, i.e. by
 21449          subtracting END_UNCHANGED we get the index of the last
 21450          unchanged character, and we have to add BEG to get its buffer
 21451          position.  */
 21452       last_unchanged_pos = Z - END_UNCHANGED + BEG;
 21453       last_unchanged_pos_old = last_unchanged_pos - *delta;
 21454 
 21455       /* Search backward from ROW for a row displaying a line that
 21456          starts at a minimum position >= last_unchanged_pos_old.  */
 21457       for (; row > first_text_row; --row)
 21458         {
 21459           /* This used to abort, but it can happen.
 21460              It is ok to just stop the search instead here.  KFS.  */
 21461           if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
 21462             break;
 21463 
 21464           if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
 21465             row_found = row;
 21466         }
 21467     }
 21468 
 21469   eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
 21470 
 21471   return row_found;
 21472 }
 21473 
 21474 
 21475 /* Make sure that glyph rows in the current matrix of window W
 21476    reference the same glyph memory as corresponding rows in the
 21477    frame's frame matrix.  This function is called after scrolling W's
 21478    current matrix on a terminal frame in try_window_id and
 21479    try_window_reusing_current_matrix.  */
 21480 
 21481 static void
 21482 sync_frame_with_window_matrix_rows (struct window *w)
 21483 {
 21484   struct frame *f = XFRAME (w->frame);
 21485   struct glyph_row *window_row, *window_row_end, *frame_row;
 21486 
 21487   /* Preconditions: W must be a leaf window and full-width.  Its frame
 21488      must have a frame matrix.  */
 21489   eassert (BUFFERP (w->contents));
 21490   eassert (WINDOW_FULL_WIDTH_P (w));
 21491   eassert (!FRAME_WINDOW_P (f));
 21492 
 21493   /* If W is a full-width window, glyph pointers in W's current matrix
 21494      have, by definition, to be the same as glyph pointers in the
 21495      corresponding frame matrix.  Note that frame matrices have no
 21496      marginal areas (see build_frame_matrix).  */
 21497   window_row = w->current_matrix->rows;
 21498   window_row_end = window_row + w->current_matrix->nrows;
 21499   frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
 21500   while (window_row < window_row_end)
 21501     {
 21502       struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
 21503       struct glyph *end = window_row->glyphs[LAST_AREA];
 21504 
 21505       frame_row->glyphs[LEFT_MARGIN_AREA] = start;
 21506       frame_row->glyphs[TEXT_AREA] = start;
 21507       frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
 21508       frame_row->glyphs[LAST_AREA] = end;
 21509 
 21510       /* Disable frame rows whose corresponding window rows have
 21511          been disabled in try_window_id.  */
 21512       if (!window_row->enabled_p)
 21513         frame_row->enabled_p = false;
 21514 
 21515       ++window_row, ++frame_row;
 21516     }
 21517 }
 21518 
 21519 
 21520 /* Find the glyph row in window W containing CHARPOS.  Consider all
 21521    rows between START and END (not inclusive).  END null means search
 21522    all rows to the end of the display area of W.  Value is the row
 21523    containing CHARPOS or null.  */
 21524 
 21525 struct glyph_row *
 21526 row_containing_pos (struct window *w, ptrdiff_t charpos,
 21527                     struct glyph_row *start, struct glyph_row *end, int dy)
 21528 {
 21529   struct glyph_row *row = start;
 21530   struct glyph_row *best_row = NULL;
 21531   ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
 21532   int last_y;
 21533 
 21534   /* If we happen to start on a header-line or a tab-line, skip that.  */
 21535   if (row->tab_line_p)
 21536     ++row;
 21537   if (row->mode_line_p)
 21538     ++row;
 21539 
 21540   if ((end && row >= end) || !row->enabled_p)
 21541     return NULL;
 21542 
 21543   last_y = window_text_bottom_y (w) - dy;
 21544 
 21545   while (true)
 21546     {
 21547       /* Give up if we have gone too far.  */
 21548       if ((end && row >= end) || !row->enabled_p)
 21549         return NULL;
 21550       /* This formerly returned if they were equal.
 21551          I think that both quantities are of a "last plus one" type;
 21552          if so, when they are equal, the row is within the screen. -- rms.  */
 21553       if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
 21554         return NULL;
 21555 
 21556       /* If it is in this row, return this row.  */
 21557       if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
 21558              || (MATRIX_ROW_END_CHARPOS (row) == charpos
 21559                  /* The end position of a row equals the start
 21560                     position of the next row.  If CHARPOS is there, we
 21561                     would rather consider it displayed in the next
 21562                     line, except when this line ends in ZV.  */
 21563                  && !row_for_charpos_p (row, charpos)))
 21564           && charpos >= MATRIX_ROW_START_CHARPOS (row))
 21565         {
 21566           struct glyph *g;
 21567 
 21568           if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21569               || (!best_row && !row->continued_p))
 21570             return row;
 21571           /* In bidi-reordered rows, there could be several rows whose
 21572              edges surround CHARPOS, all of these rows belonging to
 21573              the same continued line.  We need to find the row which
 21574              fits CHARPOS the best.  */
 21575           for (g = row->glyphs[TEXT_AREA];
 21576                g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 21577                g++)
 21578             {
 21579               if (!STRINGP (g->object))
 21580                 {
 21581                   if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
 21582                     {
 21583                       mindif = eabs (g->charpos - charpos);
 21584                       best_row = row;
 21585                       /* Exact match always wins.  */
 21586                       if (mindif == 0)
 21587                         return best_row;
 21588                     }
 21589                 }
 21590             }
 21591         }
 21592       else if (best_row && !row->continued_p)
 21593         return best_row;
 21594       ++row;
 21595     }
 21596 }
 21597 
 21598 
 21599 /* Try to redisplay window W by reusing its existing display.  W's
 21600    current matrix must be up to date when this function is called,
 21601    i.e., window_end_valid must be true.
 21602 
 21603    Value is
 21604 
 21605    >= 1 if successful, i.e. display has been updated
 21606          specifically:
 21607          1 means the changes were in front of a newline that precedes
 21608            the window start, and the whole current matrix was reused
 21609          2 means the changes were after the last position displayed
 21610            in the window, and the whole current matrix was reused
 21611          3 means portions of the current matrix were reused, while
 21612            some of the screen lines were redrawn
 21613    -1   if redisplay with same window start is known not to succeed
 21614    0    if otherwise unsuccessful
 21615 
 21616    The following steps are performed:
 21617 
 21618    1. Find the last row in the current matrix of W that is not
 21619    affected by changes at the start of current_buffer.  If no such row
 21620    is found, give up.
 21621 
 21622    2. Find the first row in W's current matrix that is not affected by
 21623    changes at the end of current_buffer.  Maybe there is no such row.
 21624 
 21625    3. Display lines beginning with the row + 1 found in step 1 to the
 21626    row found in step 2 or, if step 2 didn't find a row, to the end of
 21627    the window.
 21628 
 21629    4. If cursor is not known to appear on the window, give up.
 21630 
 21631    5. If display stopped at the row found in step 2, scroll the
 21632    display and current matrix as needed.
 21633 
 21634    6. Maybe display some lines at the end of W, if we must.  This can
 21635    happen under various circumstances, like a partially visible line
 21636    becoming fully visible, or because newly displayed lines are displayed
 21637    in smaller font sizes.
 21638 
 21639    7. Update W's window end information.  */
 21640 
 21641 static int
 21642 try_window_id (struct window *w)
 21643 {
 21644   struct frame *f = XFRAME (w->frame);
 21645   struct glyph_matrix *current_matrix = w->current_matrix;
 21646   struct glyph_matrix *desired_matrix = w->desired_matrix;
 21647   struct glyph_row *last_unchanged_at_beg_row;
 21648   struct glyph_row *first_unchanged_at_end_row;
 21649   struct glyph_row *row;
 21650   struct glyph_row *bottom_row;
 21651   int bottom_vpos;
 21652   struct it it;
 21653   ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
 21654   int dvpos, dy;
 21655   struct text_pos start_pos;
 21656   struct run run;
 21657   int first_unchanged_at_end_vpos = 0;
 21658   struct glyph_row *last_text_row, *last_text_row_at_end;
 21659   struct text_pos start;
 21660   ptrdiff_t first_changed_charpos, last_changed_charpos;
 21661 
 21662 #ifdef GLYPH_DEBUG
 21663   if (inhibit_try_window_id)
 21664     return 0;
 21665 #endif
 21666 
 21667   /* This is handy for debugging.  */
 21668 #if false
 21669 #define GIVE_UP(X)                                              \
 21670   do {                                                          \
 21671     redisplay_trace ("try_window_id give up %d\n", X);          \
 21672     return 0;                                                   \
 21673   } while (false)
 21674 #else
 21675 #define GIVE_UP(X) return 0
 21676 #endif
 21677 
 21678   SET_TEXT_POS_FROM_MARKER (start, w->start);
 21679 
 21680   /* Don't use this for mini-windows because these can show
 21681      messages and mini-buffers, and we don't handle that here.  */
 21682   if (MINI_WINDOW_P (w))
 21683     GIVE_UP (1);
 21684 
 21685   /* This flag is used to prevent redisplay optimizations.  */
 21686   if (windows_or_buffers_changed || f->cursor_type_changed)
 21687     GIVE_UP (2);
 21688 
 21689   /* This function's optimizations cannot be used if overlays have
 21690      changed in the buffer displayed by the window, so give up if they
 21691      have.  */
 21692   if (w->last_overlay_modified != OVERLAY_MODIFF)
 21693     GIVE_UP (200);
 21694 
 21695   /* Verify that narrowing has not changed.
 21696      Also verify that we were not told to prevent redisplay optimizations.
 21697      It would be nice to further
 21698      reduce the number of cases where this prevents try_window_id.  */
 21699   if (current_buffer->clip_changed
 21700       || current_buffer->prevent_redisplay_optimizations_p)
 21701     GIVE_UP (3);
 21702 
 21703   /* Window must either use window-based redisplay or be full width.  */
 21704   if (!FRAME_WINDOW_P (f)
 21705       && (!FRAME_LINE_INS_DEL_OK (f)
 21706           || !WINDOW_FULL_WIDTH_P (w)))
 21707     GIVE_UP (4);
 21708 
 21709   /* Give up if point is known NOT to appear in W.  */
 21710   if (PT < CHARPOS (start))
 21711     GIVE_UP (5);
 21712 
 21713   /* Another way to prevent redisplay optimizations.  */
 21714   if (w->last_modified == 0)
 21715     GIVE_UP (6);
 21716 
 21717   /* Verify that window is not hscrolled.  */
 21718   if (w->hscroll != 0)
 21719     GIVE_UP (7);
 21720 
 21721   /* Verify that display wasn't paused.  */
 21722   if (!w->window_end_valid)
 21723     GIVE_UP (8);
 21724 
 21725   /* Likewise if highlighting trailing whitespace.  */
 21726   if (!NILP (Vshow_trailing_whitespace))
 21727     GIVE_UP (11);
 21728 
 21729   /* Can't use this if overlay arrow position and/or string have
 21730      changed.  */
 21731   if (overlay_arrows_changed_p (false))
 21732     GIVE_UP (12);
 21733 
 21734   /* When word-wrap is on, adding a space to the first word of a
 21735      wrapped line can change the wrap position, altering the line
 21736      above it.  It might be worthwhile to handle this more
 21737      intelligently, but for now just redisplay from scratch.  */
 21738   if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
 21739     GIVE_UP (21);
 21740 
 21741   /* Under bidi reordering, adding or deleting a character in the
 21742      beginning of a paragraph, before the first strong directional
 21743      character, can change the base direction of the paragraph (unless
 21744      the buffer specifies a fixed paragraph direction), which will
 21745      require redisplaying the whole paragraph.  It might be worthwhile
 21746      to find the paragraph limits and widen the range of redisplayed
 21747      lines to that, but for now just give up this optimization and
 21748      redisplay from scratch.  */
 21749   if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
 21750       && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
 21751     GIVE_UP (22);
 21752 
 21753   /* Give up if the buffer has line-spacing set, as Lisp-level changes
 21754      to that variable require thorough redisplay.  */
 21755   if (!NILP (BVAR (XBUFFER (w->contents), extra_line_spacing)))
 21756     GIVE_UP (23);
 21757 
 21758   /* Give up if display-line-numbers is in relative mode, or when the
 21759      current line's number needs to be displayed in a distinct face.  */
 21760   if (EQ (Vdisplay_line_numbers, Qrelative)
 21761       || EQ (Vdisplay_line_numbers, Qvisual)
 21762       || (!NILP (Vdisplay_line_numbers)
 21763           && NILP (Finternal_lisp_face_equal_p (Qline_number,
 21764                                                 Qline_number_current_line,
 21765                                                 w->frame))))
 21766     GIVE_UP (24);
 21767 
 21768   /* composition-break-at-point is incompatible with the optimizations
 21769      in this function, because we need to recompose characters when
 21770      point moves off their positions.  */
 21771   if (composition_break_at_point)
 21772     GIVE_UP (27);
 21773 
 21774   /* Make sure beg_unchanged and end_unchanged are up to date.  Do it
 21775      only if buffer has really changed.  The reason is that the gap is
 21776      initially at Z for freshly visited files.  The code below would
 21777      set end_unchanged to 0 in that case.  */
 21778   if (MODIFF > SAVE_MODIFF
 21779       /* This seems to happen sometimes after saving a buffer.  */
 21780       || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
 21781     {
 21782       if (GPT - BEG < BEG_UNCHANGED)
 21783         BEG_UNCHANGED = GPT - BEG;
 21784       if (Z - GPT < END_UNCHANGED)
 21785         END_UNCHANGED = Z - GPT;
 21786     }
 21787 
 21788   /* The position of the first and last character that has been changed.  */
 21789   first_changed_charpos = BEG + BEG_UNCHANGED;
 21790   last_changed_charpos  = Z - END_UNCHANGED;
 21791 
 21792   /* If window starts after a line end, and the last change is in
 21793      front of that newline, then changes don't affect the display.
 21794      This case happens with stealth-fontification.  Note that although
 21795      the display is unchanged, glyph positions in the matrix have to
 21796      be adjusted, of course.  */
 21797   row = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 21798   if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
 21799       && ((last_changed_charpos < CHARPOS (start)
 21800            && CHARPOS (start) == BEGV)
 21801           || (last_changed_charpos < CHARPOS (start) - 1
 21802               && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
 21803     {
 21804       ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
 21805       struct glyph_row *r0;
 21806 
 21807       /* Compute how many chars/bytes have been added to or removed
 21808          from the buffer.  */
 21809       Z_old = MATRIX_ROW_END_CHARPOS (row) + w->window_end_pos;
 21810       Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
 21811       Z_delta = Z - Z_old;
 21812       Z_delta_bytes = Z_BYTE - Z_BYTE_old;
 21813 
 21814       /* Give up if PT is not in the window.  Note that it already has
 21815          been checked at the start of try_window_id that PT is not in
 21816          front of the window start.  */
 21817       if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
 21818         GIVE_UP (13);
 21819 
 21820       /* If window start is unchanged, we can reuse the whole matrix
 21821          as is, after adjusting glyph positions.  No need to compute
 21822          the window end again, since its offset from Z hasn't changed.  */
 21823       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21824       if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
 21825           && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
 21826           /* PT must not be in a partially visible line.  */
 21827           && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
 21828                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21829         {
 21830           /* Adjust positions in the glyph matrix.  */
 21831           if (Z_delta || Z_delta_bytes)
 21832             {
 21833               struct glyph_row *r1
 21834                 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 21835               increment_matrix_positions (w->current_matrix,
 21836                                           MATRIX_ROW_VPOS (r0, current_matrix),
 21837                                           MATRIX_ROW_VPOS (r1, current_matrix),
 21838                                           Z_delta, Z_delta_bytes);
 21839             }
 21840 
 21841           /* Set the cursor.  */
 21842           row = row_containing_pos (w, PT, r0, NULL, 0);
 21843           if (row)
 21844             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21845           return 1;
 21846         }
 21847     }
 21848 
 21849   /* Handle the case that changes are all below what is displayed in
 21850      the window, and that PT is in the window.  This shortcut cannot
 21851      be taken if ZV is visible in the window, and text has been added
 21852      there that is visible in the window.  */
 21853   if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
 21854       /* ZV is not visible in the window, or there are no
 21855          changes at ZV, actually.  */
 21856       && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
 21857           || first_changed_charpos == last_changed_charpos))
 21858     {
 21859       struct glyph_row *r0;
 21860 
 21861       /* Give up if PT is not in the window.  Note that it already has
 21862          been checked at the start of try_window_id that PT is not in
 21863          front of the window start.  */
 21864       if (PT >= MATRIX_ROW_END_CHARPOS (row))
 21865         GIVE_UP (14);
 21866 
 21867       /* If window start is unchanged, we can reuse the whole matrix
 21868          as is, without changing glyph positions since no text has
 21869          been added/removed in front of the window end.  */
 21870       r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21871       if (TEXT_POS_EQUAL_P (start, r0->minpos)
 21872           /* PT must not be in a partially visible line.  */
 21873           && !(PT >= MATRIX_ROW_START_CHARPOS (row)
 21874                && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
 21875         {
 21876           /* We have to compute the window end anew since text
 21877              could have been added/removed after it.  */
 21878           w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 21879           w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 21880 
 21881           /* Set the cursor.  */
 21882           row = row_containing_pos (w, PT, r0, NULL, 0);
 21883           if (row)
 21884             set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
 21885           return 2;
 21886         }
 21887     }
 21888 
 21889   /* Give up if window start is in the changed area.
 21890 
 21891      The condition used to read
 21892 
 21893      (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
 21894 
 21895      but why that was tested escapes me at the moment.  */
 21896   if (CHARPOS (start) >= first_changed_charpos
 21897       && CHARPOS (start) <= last_changed_charpos)
 21898     GIVE_UP (15);
 21899 
 21900   /* Check that window start agrees with the start of the first glyph
 21901      row in its current matrix.  Check this after we know the window
 21902      start is not in changed text, otherwise positions would not be
 21903      comparable.  */
 21904   row = MATRIX_FIRST_TEXT_ROW (current_matrix);
 21905   if (!TEXT_POS_EQUAL_P (start, row->minpos))
 21906     GIVE_UP (16);
 21907 
 21908   /* Give up if the window ends in strings.  Overlay strings
 21909      at the end are difficult to handle, so don't try.  */
 21910   row = MATRIX_ROW (current_matrix, w->window_end_vpos);
 21911   if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
 21912     GIVE_UP (20);
 21913 
 21914   /* Can't let scroll_run_hook below run on w32 GUI frames when
 21915      position of point is indicated by the system caret, because
 21916      scrolling the display will then "copy" the pixels used by the
 21917      caret.  */
 21918 #ifdef HAVE_NTGUI
 21919   if (FRAME_W32_P (f) && w32_use_visible_system_caret)
 21920     GIVE_UP (25);
 21921 #endif
 21922 
 21923   /* Compute the position at which we have to start displaying new
 21924      lines.  Some of the lines at the top of the window might be
 21925      reusable because they are not displaying changed text.  Find the
 21926      last row in W's current matrix not affected by changes at the
 21927      start of current_buffer.  Value is null if changes start in the
 21928      first line of window.  */
 21929   last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
 21930   if (last_unchanged_at_beg_row)
 21931     {
 21932       /* Avoid starting to display in the middle of a character, a TAB
 21933          for instance.  This is easier than to set up the iterator
 21934          exactly, and it's not a frequent case, so the additional
 21935          effort wouldn't really pay off.  */
 21936       while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
 21937               || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
 21938              && last_unchanged_at_beg_row > w->current_matrix->rows)
 21939         --last_unchanged_at_beg_row;
 21940 
 21941       if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
 21942         GIVE_UP (17);
 21943 
 21944       if (! init_to_row_end (&it, w, last_unchanged_at_beg_row))
 21945         GIVE_UP (18);
 21946       /* Give up if the row starts with a display property that draws
 21947          on the fringes, since that could prevent correct display of
 21948          line-prefix and wrap-prefix.  */
 21949       if (it.sp > 1
 21950           && it.method == GET_FROM_IMAGE && it.image_id == -1)
 21951         GIVE_UP (26);
 21952       start_pos = it.current.pos;
 21953 
 21954       /* Start displaying new lines in the desired matrix at the same
 21955          vpos we would use in the current matrix, i.e. below
 21956          last_unchanged_at_beg_row.  */
 21957       it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
 21958                                      current_matrix);
 21959       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 21960       it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
 21961 
 21962       eassert (it.hpos == 0 && it.current_x == 0);
 21963     }
 21964   else
 21965     {
 21966       /* There are no reusable lines at the start of the window.
 21967          Start displaying in the first text line.  */
 21968       start_display (&it, w, start);
 21969       it.vpos = it.first_vpos;
 21970       start_pos = it.current.pos;
 21971     }
 21972 
 21973   /* Find the first row that is not affected by changes at the end of
 21974      the buffer.  Value will be null if there is no unchanged row, in
 21975      which case we must redisplay to the end of the window.  delta
 21976      will be set to the value by which buffer positions beginning with
 21977      first_unchanged_at_end_row have to be adjusted due to text
 21978      changes.  */
 21979   first_unchanged_at_end_row
 21980     = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
 21981   IF_DEBUG (debug_delta = delta);
 21982   IF_DEBUG (debug_delta_bytes = delta_bytes);
 21983 
 21984   /* Set stop_pos to the buffer position up to which we will have to
 21985      display new lines.  If first_unchanged_at_end_row != NULL, this
 21986      is the buffer position of the start of the line displayed in that
 21987      row.  For first_unchanged_at_end_row == NULL, use 0 to indicate
 21988      that we don't stop at a buffer position.  */
 21989   stop_pos = 0;
 21990   if (first_unchanged_at_end_row)
 21991     {
 21992       eassert (last_unchanged_at_beg_row == NULL
 21993                || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
 21994 
 21995       /* If this is a continuation line, move forward to the next one
 21996          that isn't.  Changes in lines above affect this line.
 21997          Caution: this may move first_unchanged_at_end_row to a row
 21998          not displaying text.  */
 21999       while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
 22000              && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22001              && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22002                  < it.last_visible_y))
 22003         ++first_unchanged_at_end_row;
 22004 
 22005       if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
 22006           || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
 22007               >= it.last_visible_y))
 22008         first_unchanged_at_end_row = NULL;
 22009       else
 22010         {
 22011           stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
 22012                       + delta);
 22013           first_unchanged_at_end_vpos
 22014             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
 22015           eassert (stop_pos >= Z - END_UNCHANGED);
 22016         }
 22017     }
 22018   else if (last_unchanged_at_beg_row == NULL)
 22019     GIVE_UP (19);
 22020 
 22021 
 22022 #ifdef GLYPH_DEBUG
 22023 
 22024   /* Either there is no unchanged row at the end, or the one we have
 22025      now displays text.  This is a necessary condition for the window
 22026      end pos calculation at the end of this function.  */
 22027   eassert (first_unchanged_at_end_row == NULL
 22028            || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22029 
 22030   debug_last_unchanged_at_beg_vpos
 22031     = (last_unchanged_at_beg_row
 22032        ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
 22033        : -1);
 22034   debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
 22035 
 22036 #endif /* GLYPH_DEBUG */
 22037 
 22038 
 22039   /* Display new lines.  Set last_text_row to the last new line
 22040      displayed which has text on it, i.e. might end up as being the
 22041      line where the window_end_vpos is.  */
 22042   w->cursor.vpos = -1;
 22043   last_text_row = NULL;
 22044   overlay_arrow_seen = false;
 22045   if (it.current_y < it.last_visible_y
 22046       && !f->fonts_changed
 22047       && (first_unchanged_at_end_row == NULL
 22048           || IT_CHARPOS (it) < stop_pos))
 22049     it.glyph_row->reversed_p = false;
 22050   while (it.current_y < it.last_visible_y
 22051          && !f->fonts_changed
 22052          && (first_unchanged_at_end_row == NULL
 22053              || IT_CHARPOS (it) < stop_pos))
 22054     {
 22055       if (display_line (&it, -1))
 22056         last_text_row = it.glyph_row - 1;
 22057     }
 22058 
 22059   if (f->fonts_changed)
 22060     return -1;
 22061 
 22062   /* The redisplay iterations in display_line above could have
 22063      triggered font-lock, which could have done something that
 22064      invalidates IT->w window's end-point information, on which we
 22065      rely below.  E.g., one package, which will remain unnamed, used
 22066      to install a font-lock-fontify-region-function that called
 22067      bury-buffer, whose side effect is to switch the buffer displayed
 22068      by IT->w, and that predictably resets IT->w's window_end_valid
 22069      flag, which we already tested at the entry to this function.
 22070      Amply punish such packages/modes by giving up on this
 22071      optimization in those cases.  */
 22072   if (!w->window_end_valid)
 22073     {
 22074       clear_glyph_matrix (w->desired_matrix);
 22075       return -1;
 22076     }
 22077 
 22078   /* Compute differences in buffer positions, y-positions etc.  for
 22079      lines reused at the bottom of the window.  Compute what we can
 22080      scroll.  */
 22081   if (first_unchanged_at_end_row
 22082       /* No lines reused because we displayed everything up to the
 22083          bottom of the window.  */
 22084       && it.current_y < it.last_visible_y)
 22085     {
 22086       dvpos = (it.vpos
 22087                - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
 22088                                   current_matrix));
 22089       dy = it.current_y - first_unchanged_at_end_row->y;
 22090       run.current_y = first_unchanged_at_end_row->y;
 22091       run.desired_y = run.current_y + dy;
 22092       run.height = it.last_visible_y - max (run.current_y, run.desired_y);
 22093     }
 22094   else
 22095     {
 22096       delta = delta_bytes = dvpos = dy
 22097         = run.current_y = run.desired_y = run.height = 0;
 22098       first_unchanged_at_end_row = NULL;
 22099     }
 22100   IF_DEBUG ((debug_dvpos = dvpos, debug_dy = dy));
 22101 
 22102 
 22103   /* Find the cursor if not already found.  We have to decide whether
 22104      PT will appear on this window (it sometimes doesn't, but this is
 22105      not a very frequent case.)  This decision has to be made before
 22106      the current matrix is altered.  A value of cursor.vpos < 0 means
 22107      that PT is either in one of the lines beginning at
 22108      first_unchanged_at_end_row or below the window.  Don't care for
 22109      lines that might be displayed later at the window end; as
 22110      mentioned, this is not a frequent case.  */
 22111   if (w->cursor.vpos < 0)
 22112     {
 22113       /* Cursor in unchanged rows at the top?  */
 22114       if (PT < CHARPOS (start_pos)
 22115           && last_unchanged_at_beg_row)
 22116         {
 22117           row = row_containing_pos (w, PT,
 22118                                     MATRIX_FIRST_TEXT_ROW (w->current_matrix),
 22119                                     last_unchanged_at_beg_row + 1, 0);
 22120           if (row)
 22121             set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
 22122         }
 22123 
 22124       /* Start from first_unchanged_at_end_row looking for PT.  */
 22125       else if (first_unchanged_at_end_row)
 22126         {
 22127           row = row_containing_pos (w, PT - delta,
 22128                                     first_unchanged_at_end_row, NULL, 0);
 22129           if (row)
 22130             set_cursor_from_row (w, row, w->current_matrix, delta,
 22131                                  delta_bytes, dy, dvpos);
 22132         }
 22133 
 22134       /* Give up if cursor was not found.  */
 22135       if (w->cursor.vpos < 0)
 22136         {
 22137           clear_glyph_matrix (w->desired_matrix);
 22138           return -1;
 22139         }
 22140     }
 22141 
 22142   /* Don't let the cursor end in the scroll margins.  */
 22143   {
 22144     int top_scroll_margin = window_scroll_margin (w, MARGIN_IN_PIXELS);
 22145     int bot_scroll_margin = top_scroll_margin;
 22146     int cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
 22147 
 22148     if (window_wants_header_line (w))
 22149       top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
 22150     if (window_wants_tab_line (w))
 22151       top_scroll_margin += CURRENT_TAB_LINE_HEIGHT (w);
 22152 
 22153     if ((w->cursor.y < top_scroll_margin
 22154          && CHARPOS (start) > BEGV)
 22155         /* Old redisplay didn't take scroll margin into account at the bottom,
 22156            but then global-hl-line-mode doesn't scroll.  KFS 2004-06-14 */
 22157         || (w->cursor.y
 22158             + (cursor_row_fully_visible_p (w, false, true, true)
 22159                ? 1
 22160                : cursor_height + bot_scroll_margin)) > it.last_visible_y)
 22161       {
 22162         w->cursor.vpos = -1;
 22163         clear_glyph_matrix (w->desired_matrix);
 22164         return -1;
 22165       }
 22166   }
 22167 
 22168   /* Scroll the display.  Do it before changing the current matrix so
 22169      that xterm.c doesn't get confused about where the cursor glyph is
 22170      found.  */
 22171   if (dy && run.height)
 22172     {
 22173       update_begin (f);
 22174 
 22175       if (FRAME_WINDOW_P (f))
 22176         {
 22177 #ifdef HAVE_WINDOW_SYSTEM
 22178           gui_update_window_begin (w);
 22179           FRAME_RIF (f)->clear_window_mouse_face (w);
 22180           FRAME_RIF (f)->scroll_run_hook (w, &run);
 22181           gui_update_window_end (w, false, false);
 22182 #endif
 22183         }
 22184       else
 22185         {
 22186           /* Terminal frame.  In this case, dvpos gives the number of
 22187              lines to scroll by; dvpos < 0 means scroll up.  */
 22188           int from_vpos
 22189             = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
 22190           int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
 22191           int end = (WINDOW_TOP_EDGE_LINE (w)
 22192                      + window_wants_tab_line (w)
 22193                      + window_wants_header_line (w)
 22194                      + window_internal_height (w));
 22195 
 22196           gui_clear_window_mouse_face (w);
 22197 
 22198           /* Perform the operation on the screen.  */
 22199           if (dvpos > 0)
 22200             {
 22201               /* Scroll last_unchanged_at_beg_row to the end of the
 22202                  window down dvpos lines.  */
 22203               set_terminal_window (f, end);
 22204 
 22205               /* On dumb terminals delete dvpos lines at the end
 22206                  before inserting dvpos empty lines.  */
 22207               if (!FRAME_SCROLL_REGION_OK (f))
 22208                 ins_del_lines (f, end - dvpos, -dvpos);
 22209 
 22210               /* Insert dvpos empty lines in front of
 22211                  last_unchanged_at_beg_row.  */
 22212               ins_del_lines (f, from, dvpos);
 22213             }
 22214           else if (dvpos < 0)
 22215             {
 22216               /* Scroll up last_unchanged_at_beg_vpos to the end of
 22217                  the window to last_unchanged_at_beg_vpos - |dvpos|.  */
 22218               set_terminal_window (f, end);
 22219 
 22220               /* Delete dvpos lines in front of
 22221                  last_unchanged_at_beg_vpos.  ins_del_lines will set
 22222                  the cursor to the given vpos and emit |dvpos| delete
 22223                  line sequences.  */
 22224               ins_del_lines (f, from + dvpos, dvpos);
 22225 
 22226               /* On a dumb terminal insert dvpos empty lines at the
 22227                  end.  */
 22228               if (!FRAME_SCROLL_REGION_OK (f))
 22229                 ins_del_lines (f, end + dvpos, -dvpos);
 22230             }
 22231 
 22232           set_terminal_window (f, 0);
 22233         }
 22234 
 22235       update_end (f);
 22236     }
 22237 
 22238   /* Shift reused rows of the current matrix to the right position.
 22239      BOTTOM_ROW is the last + 1 row in the current matrix reserved for
 22240      text.  */
 22241   bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
 22242   bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
 22243   if (dvpos < 0)
 22244     {
 22245       rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
 22246                      bottom_vpos, dvpos);
 22247       clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
 22248                                bottom_vpos);
 22249     }
 22250   else if (dvpos > 0)
 22251     {
 22252       rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
 22253                      bottom_vpos, dvpos);
 22254       clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
 22255                                first_unchanged_at_end_vpos + dvpos);
 22256     }
 22257 
 22258   /* For frame-based redisplay, make sure that current frame and window
 22259      matrix are in sync with respect to glyph memory.  */
 22260   if (!FRAME_WINDOW_P (f))
 22261     sync_frame_with_window_matrix_rows (w);
 22262 
 22263   /* Adjust buffer positions in reused rows.  */
 22264   if (delta || delta_bytes)
 22265     increment_matrix_positions (current_matrix,
 22266                                 first_unchanged_at_end_vpos + dvpos,
 22267                                 bottom_vpos, delta, delta_bytes);
 22268 
 22269   /* Adjust Y positions.  */
 22270   if (dy)
 22271     shift_glyph_matrix (w, current_matrix,
 22272                         first_unchanged_at_end_vpos + dvpos,
 22273                         bottom_vpos, dy);
 22274 
 22275   if (first_unchanged_at_end_row)
 22276     {
 22277       first_unchanged_at_end_row += dvpos;
 22278       if (first_unchanged_at_end_row->y >= it.last_visible_y
 22279           || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
 22280         first_unchanged_at_end_row = NULL;
 22281     }
 22282 
 22283   /* If scrolling up, there may be some lines to display at the end of
 22284      the window.  */
 22285   last_text_row_at_end = NULL;
 22286   if (dy < 0)
 22287     {
 22288       /* Scrolling up can leave for example a partially visible line
 22289          at the end of the window to be redisplayed.  */
 22290       /* Set last_row to the glyph row in the current matrix where the
 22291          window end line is found.  It has been moved up or down in
 22292          the matrix by dvpos.  */
 22293       int last_vpos = w->window_end_vpos + dvpos;
 22294       struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
 22295 
 22296       /* If last_row is the window end line, it should display text.  */
 22297       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
 22298 
 22299       /* If window end line was partially visible before, begin
 22300          displaying at that line.  Otherwise begin displaying with the
 22301          line following it.  */
 22302       if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
 22303         {
 22304           init_to_row_start (&it, w, last_row);
 22305           it.vpos = last_vpos;
 22306           it.current_y = last_row->y;
 22307         }
 22308       else
 22309         {
 22310           init_to_row_end (&it, w, last_row);
 22311           it.vpos = 1 + last_vpos;
 22312           it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
 22313           ++last_row;
 22314         }
 22315 
 22316       /* We may start in a continuation line.  If so, we have to
 22317          get the right continuation_lines_width and current_x.  */
 22318       it.continuation_lines_width = last_row->continuation_lines_width;
 22319       it.hpos = it.current_x = 0;
 22320 
 22321       /* Display the rest of the lines at the window end.  */
 22322       it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
 22323       while (it.current_y < it.last_visible_y && !f->fonts_changed)
 22324         {
 22325           /* Is it always sure that the display agrees with lines in
 22326              the current matrix?  I don't think so, so we mark rows
 22327              displayed invalid in the current matrix by setting their
 22328              enabled_p flag to false.  */
 22329           SET_MATRIX_ROW_ENABLED_P (w->current_matrix, it.vpos, false);
 22330           if (display_line (&it, w->cursor.vpos))
 22331             last_text_row_at_end = it.glyph_row - 1;
 22332         }
 22333     }
 22334 
 22335   /* Update window_end_pos and window_end_vpos.  */
 22336   if (first_unchanged_at_end_row && !last_text_row_at_end)
 22337     {
 22338       /* Window end line if one of the preserved rows from the current
 22339          matrix.  Set row to the last row displaying text in current
 22340          matrix starting at first_unchanged_at_end_row, after
 22341          scrolling.  */
 22342       eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
 22343       row = find_last_row_displaying_text (w->current_matrix, &it,
 22344                                            first_unchanged_at_end_row);
 22345       eassume (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
 22346       adjust_window_ends (w, row, true);
 22347       eassert (w->window_end_bytepos >= 0);
 22348       IF_DEBUG (debug_method_add (w, "A"));
 22349     }
 22350   else if (last_text_row_at_end)
 22351     {
 22352       adjust_window_ends (w, last_text_row_at_end, false);
 22353       eassert (w->window_end_bytepos >= 0);
 22354       IF_DEBUG (debug_method_add (w, "B"));
 22355     }
 22356   else if (last_text_row)
 22357     {
 22358       /* We have displayed either to the end of the window or at the
 22359          end of the window, i.e. the last row with text is to be found
 22360          in the desired matrix.  */
 22361       adjust_window_ends (w, last_text_row, false);
 22362       eassert (w->window_end_bytepos >= 0);
 22363     }
 22364   else if (first_unchanged_at_end_row == NULL
 22365            && last_text_row == NULL
 22366            && last_text_row_at_end == NULL)
 22367     {
 22368       /* Displayed to end of window, but no line containing text was
 22369          displayed.  Lines were deleted at the end of the window.  */
 22370       int first_vpos = window_wants_tab_line (w) + window_wants_header_line (w);
 22371       int vpos = w->window_end_vpos;
 22372       struct glyph_row *current_row = current_matrix->rows + vpos;
 22373       struct glyph_row *desired_row = desired_matrix->rows + vpos;
 22374 
 22375       for (row = NULL; !row; --vpos, --current_row, --desired_row)
 22376         {
 22377           eassert (first_vpos <= vpos);
 22378           if (desired_row->enabled_p)
 22379             {
 22380               if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
 22381                 row = desired_row;
 22382             }
 22383           else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
 22384             row  = current_row;
 22385         }
 22386 
 22387       w->window_end_vpos = vpos + 1;
 22388       w->window_end_pos = Z - MATRIX_ROW_END_CHARPOS (row);
 22389       w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
 22390       eassert (w->window_end_bytepos >= 0);
 22391       IF_DEBUG (debug_method_add (w, "C"));
 22392     }
 22393   else
 22394     emacs_abort ();
 22395 
 22396   IF_DEBUG ((debug_end_pos = w->window_end_pos,
 22397              debug_end_vpos = w->window_end_vpos));
 22398 
 22399   /* Record that display has not been completed.  */
 22400   w->window_end_valid = false;
 22401   w->desired_matrix->no_scrolling_p = true;
 22402   return 3;
 22403 
 22404 #undef GIVE_UP
 22405 }
 22406 
 22407 
 22408 
 22409 /***********************************************************************
 22410                         More debugging support
 22411  ***********************************************************************/
 22412 
 22413 #ifdef GLYPH_DEBUG
 22414 
 22415 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
 22416 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
 22417 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
 22418 
 22419 
 22420 /* Dump the contents of glyph matrix MATRIX on stderr.
 22421 
 22422    GLYPHS 0 means don't show glyph contents.
 22423    GLYPHS 1 means show glyphs in short form
 22424    GLYPHS > 1 means show glyphs in long form.  */
 22425 
 22426 void
 22427 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
 22428 {
 22429   int i;
 22430   for (i = 0; i < matrix->nrows; ++i)
 22431     dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
 22432 }
 22433 
 22434 
 22435 /* Dump contents of glyph GLYPH to stderr.  ROW and AREA are
 22436    the glyph row and area where the glyph comes from.  */
 22437 
 22438 void
 22439 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
 22440 {
 22441   if (glyph->type == CHAR_GLYPH
 22442       || glyph->type == GLYPHLESS_GLYPH)
 22443     {
 22444       fprintf (stderr,
 22445                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22446                glyph - row->glyphs[TEXT_AREA],
 22447                (glyph->type == CHAR_GLYPH
 22448                 ? 'C'
 22449                 : 'G'),
 22450                glyph->charpos,
 22451                (BUFFERP (glyph->object)
 22452                 ? 'B'
 22453                 : (STRINGP (glyph->object)
 22454                    ? 'S'
 22455                    : (NILP (glyph->object)
 22456                       ? '0'
 22457                       : '-'))),
 22458                glyph->pixel_width,
 22459                glyph->u.ch,
 22460                (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
 22461                 ? (int) glyph->u.ch
 22462                 : '.'),
 22463                glyph->face_id,
 22464                glyph->left_box_line_p,
 22465                glyph->right_box_line_p);
 22466     }
 22467   else if (glyph->type == STRETCH_GLYPH)
 22468     {
 22469       fprintf (stderr,
 22470                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22471                glyph - row->glyphs[TEXT_AREA],
 22472                'S',
 22473                glyph->charpos,
 22474                (BUFFERP (glyph->object)
 22475                 ? 'B'
 22476                 : (STRINGP (glyph->object)
 22477                    ? 'S'
 22478                    : (NILP (glyph->object)
 22479                       ? '0'
 22480                       : '-'))),
 22481                glyph->pixel_width,
 22482                0u,
 22483                ' ',
 22484                glyph->face_id,
 22485                glyph->left_box_line_p,
 22486                glyph->right_box_line_p);
 22487     }
 22488   else if (glyph->type == IMAGE_GLYPH)
 22489     {
 22490       fprintf (stderr,
 22491                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x      %c %4d %1.1d%1.1d\n",
 22492                glyph - row->glyphs[TEXT_AREA],
 22493                'I',
 22494                glyph->charpos,
 22495                (BUFFERP (glyph->object)
 22496                 ? 'B'
 22497                 : (STRINGP (glyph->object)
 22498                    ? 'S'
 22499                    : (NILP (glyph->object)
 22500                       ? '0'
 22501                       : '-'))),
 22502                glyph->pixel_width,
 22503                (unsigned int) glyph->u.img_id,
 22504                '.',
 22505                glyph->face_id,
 22506                glyph->left_box_line_p,
 22507                glyph->right_box_line_p);
 22508     }
 22509   else if (glyph->type == COMPOSITE_GLYPH)
 22510     {
 22511       fprintf (stderr,
 22512                "  %5"pD"d     %c %9"pD"d   %c %3d 0x%06x",
 22513                glyph - row->glyphs[TEXT_AREA],
 22514                '+',
 22515                glyph->charpos,
 22516                (BUFFERP (glyph->object)
 22517                 ? 'B'
 22518                 : (STRINGP (glyph->object)
 22519                    ? 'S'
 22520                    : (NILP (glyph->object)
 22521                       ? '0'
 22522                       : '-'))),
 22523                glyph->pixel_width,
 22524                (unsigned int) glyph->u.cmp.id);
 22525       if (glyph->u.cmp.automatic)
 22526         fprintf (stderr,
 22527                  "[%d-%d]",
 22528                  glyph->slice.cmp.from, glyph->slice.cmp.to);
 22529       fprintf (stderr, " . %4d %1.1d%1.1d\n",
 22530                glyph->face_id,
 22531                glyph->left_box_line_p,
 22532                glyph->right_box_line_p);
 22533     }
 22534   else if (glyph->type == XWIDGET_GLYPH)
 22535     {
 22536 #ifndef HAVE_XWIDGETS
 22537       eassume (false);
 22538 #else
 22539       fprintf (stderr,
 22540                "  %5td %4c %6td %c %3d %7p %c %4d %1.1d%1.1d\n",
 22541                glyph - row->glyphs[TEXT_AREA],
 22542                'X',
 22543                glyph->charpos,
 22544                (BUFFERP (glyph->object)
 22545                 ? 'B'
 22546                 : (STRINGP (glyph->object)
 22547                    ? 'S'
 22548                    : '-')),
 22549                glyph->pixel_width,
 22550                glyph->u.xwidget,
 22551                '.',
 22552                glyph->face_id,
 22553                glyph->left_box_line_p,
 22554                glyph->right_box_line_p);
 22555 #endif
 22556     }
 22557 }
 22558 
 22559 
 22560 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
 22561    GLYPHS 0 means don't show glyph contents.
 22562    GLYPHS 1 means show glyphs in short form
 22563    GLYPHS > 1 means show glyphs in long form.  */
 22564 
 22565 void
 22566 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
 22567 {
 22568   if (glyphs != 1)
 22569     {
 22570       fputs (("Row     Start       End Used oE><\\CTZFesm     X    Y    W    H    V    A    P\n"
 22571               "==============================================================================\n"),
 22572              stderr);
 22573       fprintf (stderr, "%3d %9"pD"d %9"pD"d %4d %1.1d%1.1d%1.1d%1.1d\
 22574 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d  %4d %4d %4d %4d %4d %4d %4d\n",
 22575                vpos,
 22576                MATRIX_ROW_START_CHARPOS (row),
 22577                MATRIX_ROW_END_CHARPOS (row),
 22578                row->used[TEXT_AREA],
 22579                row->contains_overlapping_glyphs_p,
 22580                row->enabled_p,
 22581                row->truncated_on_left_p,
 22582                row->truncated_on_right_p,
 22583                row->continued_p,
 22584                MATRIX_ROW_CONTINUATION_LINE_P (row),
 22585                MATRIX_ROW_DISPLAYS_TEXT_P (row),
 22586                row->ends_at_zv_p,
 22587                row->fill_line_p,
 22588                row->ends_in_middle_of_char_p,
 22589                row->starts_in_middle_of_char_p,
 22590                row->mouse_face_p,
 22591                row->x,
 22592                row->y,
 22593                row->pixel_width,
 22594                row->height,
 22595                row->visible_height,
 22596                row->ascent,
 22597                row->phys_ascent);
 22598       /* The next 3 lines should align to "Start" in the header.  */
 22599       fprintf (stderr, "    %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
 22600                row->end.overlay_string_index,
 22601                row->continuation_lines_width);
 22602       fprintf (stderr, "    %9"pD"d %9"pD"d\n",
 22603                CHARPOS (row->start.string_pos),
 22604                CHARPOS (row->end.string_pos));
 22605       fprintf (stderr, "    %9d %9d\n", row->start.dpvec_index,
 22606                row->end.dpvec_index);
 22607     }
 22608 
 22609   if (glyphs > 1)
 22610     {
 22611       int area;
 22612 
 22613       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22614         {
 22615           struct glyph *glyph = row->glyphs[area];
 22616           struct glyph *glyph_end = glyph + row->used[area];
 22617 
 22618           /* Glyph for a line end in text.  */
 22619           if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
 22620             ++glyph_end;
 22621 
 22622           if (glyph < glyph_end)
 22623             fputs (" Glyph#  Type       Pos   O   W     Code      C Face LR\n",
 22624                    stderr);
 22625 
 22626           for (; glyph < glyph_end; ++glyph)
 22627             dump_glyph (row, glyph, area);
 22628         }
 22629     }
 22630   else if (glyphs == 1)
 22631     {
 22632       int area;
 22633       char s[SHRT_MAX + 4];
 22634 
 22635       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 22636         {
 22637           int i;
 22638 
 22639           for (i = 0; i < row->used[area]; ++i)
 22640             {
 22641               struct glyph *glyph = row->glyphs[area] + i;
 22642               if (i == row->used[area] - 1
 22643                   && area == TEXT_AREA
 22644                   && NILP (glyph->object)
 22645                   && glyph->type == CHAR_GLYPH
 22646                   && glyph->u.ch == ' ')
 22647                 {
 22648                   strcpy (&s[i], "[\\n]");
 22649                   i += 4;
 22650                 }
 22651               else if (glyph->type == CHAR_GLYPH
 22652                        && glyph->u.ch < 0x80
 22653                        && glyph->u.ch >= ' ')
 22654                 s[i] = glyph->u.ch;
 22655               else
 22656                 s[i] = '.';
 22657             }
 22658 
 22659           s[i] = '\0';
 22660           fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
 22661         }
 22662     }
 22663 }
 22664 
 22665 
 22666 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
 22667        Sdump_glyph_matrix, 0, 1, "p",
 22668        doc: /* Dump the current matrix of the selected window to stderr.
 22669 Shows contents of glyph row structures.  With non-nil
 22670 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
 22671 glyphs in short form, otherwise show glyphs in long form.
 22672 
 22673 Interactively, no argument means show glyphs in short form;
 22674 with numeric argument, its value is passed as the GLYPHS flag.  */)
 22675   (Lisp_Object glyphs)
 22676 {
 22677   struct window *w = XWINDOW (selected_window);
 22678   struct buffer *buffer = XBUFFER (w->contents);
 22679 
 22680   fprintf (stderr, "PT = %"pD"d, BEGV = %"pD"d. ZV = %"pD"d\n",
 22681            BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
 22682   fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
 22683            w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
 22684   fputs ("=============================================\n", stderr);
 22685   dump_glyph_matrix (w->current_matrix,
 22686                      TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 0);
 22687   return Qnil;
 22688 }
 22689 
 22690 
 22691 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
 22692        Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
 22693 Only text-mode frames have frame glyph matrices.  */)
 22694   (void)
 22695 {
 22696   struct frame *f = XFRAME (selected_frame);
 22697 
 22698   if (f->current_matrix)
 22699     dump_glyph_matrix (f->current_matrix, 1);
 22700   else
 22701     fputs ("*** This frame doesn't have a frame glyph matrix ***\n", stderr);
 22702   return Qnil;
 22703 }
 22704 
 22705 
 22706 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "P",
 22707        doc: /* Dump glyph row ROW to stderr.
 22708 Interactively, ROW is the prefix numeric argument and defaults to
 22709 the row which displays point.
 22710 Optional argument GLYPHS 0 means don't dump glyphs.
 22711 GLYPHS 1 means dump glyphs in short form.
 22712 GLYPHS > 1 or omitted means dump glyphs in long form.  */)
 22713   (Lisp_Object row, Lisp_Object glyphs)
 22714 {
 22715   struct glyph_matrix *matrix;
 22716   EMACS_INT vpos;
 22717 
 22718   if (NILP (row))
 22719     {
 22720       int d1, d2, d3, d4, d5, ypos;
 22721       bool visible_p = pos_visible_p (XWINDOW (selected_window), PT,
 22722                                       &d1, &d2, &d3, &d4, &d5, &ypos);
 22723       if (visible_p)
 22724         vpos = ypos;
 22725       else
 22726         vpos = 0;
 22727     }
 22728   else
 22729     {
 22730       CHECK_FIXNUM (row);
 22731       vpos = XFIXNUM (row);
 22732     }
 22733   matrix = XWINDOW (selected_window)->current_matrix;
 22734   if (vpos >= 0 && vpos < matrix->nrows)
 22735     dump_glyph_row (MATRIX_ROW (matrix, vpos),
 22736                     vpos,
 22737                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22738   return Qnil;
 22739 }
 22740 
 22741 
 22742 DEFUN ("dump-tab-bar-row", Fdump_tab_bar_row, Sdump_tab_bar_row, 1, 2, "P",
 22743        doc: /* Dump glyph row ROW of the tab-bar of the current frame to stderr.
 22744 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22745 GLYPHS 0 means don't dump glyphs.
 22746 GLYPHS 1 means dump glyphs in short form.
 22747 GLYPHS > 1 or omitted means dump glyphs in long form.
 22748 
 22749 If there's no tab-bar, or if the tab-bar is not drawn by Emacs,
 22750 do nothing.  */)
 22751   (Lisp_Object row, Lisp_Object glyphs)
 22752 {
 22753 #if defined (HAVE_WINDOW_SYSTEM)
 22754   struct frame *sf = SELECTED_FRAME ();
 22755   struct glyph_matrix *m = WINDOWP (sf->tab_bar_window)
 22756     ? XWINDOW (sf->tab_bar_window)->current_matrix
 22757     : sf->current_matrix;
 22758   EMACS_INT vpos;
 22759 
 22760   if (NILP (row))
 22761     vpos = WINDOWP (sf->tab_bar_window) ? 0 :
 22762       FRAME_MENU_BAR_LINES (sf) > 0 ? 1 : 0;
 22763   else
 22764     {
 22765       CHECK_FIXNUM (row);
 22766       vpos = XFIXNUM (row);
 22767     }
 22768   if (vpos >= 0 && vpos < m->nrows)
 22769     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22770                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22771 #endif
 22772   return Qnil;
 22773 }
 22774 
 22775 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "P",
 22776        doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
 22777 Interactively, ROW is the prefix numeric argument and defaults to zero.
 22778 GLYPHS 0 means don't dump glyphs.
 22779 GLYPHS 1 means dump glyphs in short form.
 22780 GLYPHS > 1 or omitted means dump glyphs in long form.
 22781 
 22782 If there's no tool-bar, or if the tool-bar is not drawn by Emacs,
 22783 do nothing.  */)
 22784   (Lisp_Object row, Lisp_Object glyphs)
 22785 {
 22786 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 22787   struct frame *sf = SELECTED_FRAME ();
 22788   struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
 22789   EMACS_INT vpos;
 22790 
 22791   if (NILP (row))
 22792     vpos = 0;
 22793   else
 22794     {
 22795       CHECK_FIXNUM (row);
 22796       vpos = XFIXNUM (row);
 22797     }
 22798   if (vpos >= 0 && vpos < m->nrows)
 22799     dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
 22800                     TYPE_RANGED_FIXNUMP (int, glyphs) ? XFIXNUM (glyphs) : 2);
 22801 #endif
 22802   return Qnil;
 22803 }
 22804 
 22805 
 22806 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
 22807        doc: /* Toggle tracing of redisplay.
 22808 With ARG, turn tracing on if and only if ARG is positive.  */)
 22809   (Lisp_Object arg)
 22810 {
 22811   if (NILP (arg))
 22812     trace_redisplay_p = !trace_redisplay_p;
 22813   else
 22814     {
 22815       arg = Fprefix_numeric_value (arg);
 22816       trace_redisplay_p = XFIXNUM (arg) > 0;
 22817     }
 22818 
 22819   return Qnil;
 22820 }
 22821 
 22822 
 22823 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
 22824        doc: /* Like `format', but print result to stderr.
 22825 usage: (trace-to-stderr STRING &rest OBJECTS)  */)
 22826   (ptrdiff_t nargs, Lisp_Object *args)
 22827 {
 22828   Lisp_Object s = Fformat (nargs, args);
 22829   fwrite (SDATA (s), 1, SBYTES (s), stderr);
 22830   return Qnil;
 22831 }
 22832 
 22833 #endif /* GLYPH_DEBUG */
 22834 
 22835 
 22836 
 22837 /***********************************************************************
 22838                      Building Desired Matrix Rows
 22839  ***********************************************************************/
 22840 
 22841 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
 22842    Used for non-window-redisplay windows, and for windows without left
 22843    fringe.  */
 22844 
 22845 static struct glyph_row *
 22846 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 22847 {
 22848   struct frame *f = XFRAME (WINDOW_FRAME (w));
 22849   struct buffer *buffer = XBUFFER (w->contents);
 22850   struct buffer *old = current_buffer;
 22851   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
 22852   ptrdiff_t arrow_len = SBYTES (overlay_arrow_string), char_num = 0;
 22853   const unsigned char *arrow_end = arrow_string + arrow_len;
 22854   const unsigned char *p;
 22855   struct it it;
 22856   bool multibyte_p;
 22857   int n_glyphs_before;
 22858 
 22859   set_buffer_temp (buffer);
 22860   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
 22861   scratch_glyph_row.reversed_p = false;
 22862   it.glyph_row->used[TEXT_AREA] = 0;
 22863   SET_TEXT_POS (it.position, 0, 0);
 22864 
 22865   multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
 22866   p = arrow_string;
 22867   while (p < arrow_end)
 22868     {
 22869       Lisp_Object face, ilisp;
 22870 
 22871       /* Get the next character.  */
 22872       if (multibyte_p)
 22873         it.c = it.char_to_display = check_char_and_length (p, &it.len);
 22874       else
 22875         {
 22876           it.c = it.char_to_display = *p, it.len = 1;
 22877           if (! ASCII_CHAR_P (it.c))
 22878             it.char_to_display = BYTE8_TO_CHAR (it.c);
 22879         }
 22880       p += it.len;
 22881 
 22882       /* Get its face.  */
 22883       ilisp = make_fixnum (char_num++);
 22884       face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
 22885       it.face_id = compute_char_face (f, it.char_to_display, face);
 22886 
 22887       /* Compute its width, get its glyphs.  */
 22888       n_glyphs_before = it.glyph_row->used[TEXT_AREA];
 22889       SET_TEXT_POS (it.position, -1, -1);
 22890       PRODUCE_GLYPHS (&it);
 22891 
 22892       /* If this character doesn't fit any more in the line, we have
 22893          to remove some glyphs.  */
 22894       if (it.current_x > it.last_visible_x)
 22895         {
 22896           it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
 22897           break;
 22898         }
 22899     }
 22900 
 22901   set_buffer_temp (old);
 22902   return it.glyph_row;
 22903 }
 22904 
 22905 
 22906 /* Insert truncation glyphs at the start of IT->glyph_row.  Which
 22907    glyphs to insert is determined by produce_special_glyphs.  */
 22908 
 22909 static void
 22910 insert_left_trunc_glyphs (struct it *it)
 22911 {
 22912   struct it truncate_it;
 22913   struct glyph *from, *end, *to, *toend;
 22914 
 22915   eassert (!FRAME_WINDOW_P (it->f)
 22916            || (!it->glyph_row->reversed_p
 22917                && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 22918            || (it->glyph_row->reversed_p
 22919                && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
 22920 
 22921   /* Get the truncation glyphs.  */
 22922   truncate_it = *it;
 22923   truncate_it.current_x = 0;
 22924   truncate_it.face_id = DEFAULT_FACE_ID;
 22925   truncate_it.glyph_row = &scratch_glyph_row;
 22926   truncate_it.area = TEXT_AREA;
 22927   truncate_it.glyph_row->used[TEXT_AREA] = 0;
 22928   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
 22929   truncate_it.object = Qnil;
 22930   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 22931 
 22932   /* Overwrite glyphs from IT with truncation glyphs.  */
 22933   if (!it->glyph_row->reversed_p)
 22934     {
 22935       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 22936 
 22937       from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22938       end = from + tused;
 22939       to = it->glyph_row->glyphs[TEXT_AREA];
 22940       toend = to + it->glyph_row->used[TEXT_AREA];
 22941       if (FRAME_WINDOW_P (it->f))
 22942         {
 22943           /* On GUI frames, when variable-size fonts are displayed,
 22944              the truncation glyphs may need more pixels than the row's
 22945              glyphs they overwrite.  We overwrite more glyphs to free
 22946              enough screen real estate, and enlarge the stretch glyph
 22947              on the right (see display_line), if there is one, to
 22948              preserve the screen position of the truncation glyphs on
 22949              the right.  */
 22950           int w = 0;
 22951           struct glyph *g = to;
 22952           short used;
 22953 
 22954           /* The first glyph could be partially visible, in which case
 22955              it->glyph_row->x will be negative.  But we want the left
 22956              truncation glyphs to be aligned at the left margin of the
 22957              window, so we override the x coordinate at which the row
 22958              will begin.  */
 22959           it->glyph_row->x = 0;
 22960           while (g < toend && w < it->truncation_pixel_width)
 22961             {
 22962               w += g->pixel_width;
 22963               ++g;
 22964             }
 22965           if (g - to - tused > 0)
 22966             {
 22967               memmove (to + tused, g, (toend - g) * sizeof(*g));
 22968               it->glyph_row->used[TEXT_AREA] -= g - to - tused;
 22969             }
 22970           used = it->glyph_row->used[TEXT_AREA];
 22971           if (it->glyph_row->truncated_on_right_p
 22972               && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
 22973               && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
 22974               == STRETCH_GLYPH)
 22975             {
 22976               int extra = w - it->truncation_pixel_width;
 22977 
 22978               it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
 22979             }
 22980         }
 22981 
 22982       while (from < end)
 22983         *to++ = *from++;
 22984 
 22985       /* There may be padding glyphs left over.  Overwrite them too.  */
 22986       if (!FRAME_WINDOW_P (it->f))
 22987         {
 22988           while (to < toend && CHAR_GLYPH_PADDING_P (*to))
 22989             {
 22990               from = truncate_it.glyph_row->glyphs[TEXT_AREA];
 22991               while (from < end)
 22992                 *to++ = *from++;
 22993             }
 22994         }
 22995 
 22996       if (to > toend)
 22997         it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
 22998     }
 22999   else
 23000     {
 23001       short tused = truncate_it.glyph_row->used[TEXT_AREA];
 23002 
 23003       /* In R2L rows, overwrite the last (rightmost) glyphs, and do
 23004          that back to front.  */
 23005       end = truncate_it.glyph_row->glyphs[TEXT_AREA];
 23006       from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23007       toend = it->glyph_row->glyphs[TEXT_AREA];
 23008       to = toend + it->glyph_row->used[TEXT_AREA] - 1;
 23009       if (FRAME_WINDOW_P (it->f))
 23010         {
 23011           int w = 0;
 23012           struct glyph *g = to;
 23013 
 23014           while (g >= toend && w < it->truncation_pixel_width)
 23015             {
 23016               w += g->pixel_width;
 23017               --g;
 23018             }
 23019           if (to - g - tused > 0)
 23020             to = g + tused;
 23021           if (it->glyph_row->truncated_on_right_p
 23022               && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
 23023               && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
 23024             {
 23025               int extra = w - it->truncation_pixel_width;
 23026 
 23027               it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
 23028             }
 23029         }
 23030 
 23031       while (from >= end && to >= toend)
 23032         *to-- = *from--;
 23033       if (!FRAME_WINDOW_P (it->f))
 23034         {
 23035           while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
 23036             {
 23037               from =
 23038                 truncate_it.glyph_row->glyphs[TEXT_AREA]
 23039                 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
 23040               while (from >= end && to >= toend)
 23041                 *to-- = *from--;
 23042             }
 23043         }
 23044       if (from >= end)
 23045         {
 23046           /* Need to free some room before prepending additional
 23047              glyphs.  */
 23048           int move_by = from - end + 1;
 23049           struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
 23050           struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
 23051 
 23052           for ( ; g >= g0; g--)
 23053             g[move_by] = *g;
 23054           while (from >= end)
 23055             *to-- = *from--;
 23056           it->glyph_row->used[TEXT_AREA] += move_by;
 23057         }
 23058     }
 23059 }
 23060 
 23061 /* Compute the hash code for ROW.  */
 23062 unsigned
 23063 row_hash (struct glyph_row *row)
 23064 {
 23065   int area, k;
 23066   unsigned hashval = 0;
 23067 
 23068   for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
 23069     for (k = 0; k < row->used[area]; ++k)
 23070       hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
 23071                   + row->glyphs[area][k].u.val
 23072                   + row->glyphs[area][k].face_id
 23073                   + row->glyphs[area][k].padding_p
 23074                   + (row->glyphs[area][k].type << 2));
 23075 
 23076   return hashval;
 23077 }
 23078 
 23079 /* Compute the pixel height and width of IT->glyph_row.
 23080 
 23081    Most of the time, ascent and height of a display line will be equal
 23082    to the max_ascent and max_height values of the display iterator
 23083    structure.  This is not the case if
 23084 
 23085    1. We hit ZV without displaying anything.  In this case, max_ascent
 23086    and max_height will be zero.
 23087 
 23088    2. We have some glyphs that don't contribute to the line height.
 23089    (The glyph row flag contributes_to_line_height_p is for future
 23090    pixmap extensions).
 23091 
 23092    The first case is easily covered by using default values because in
 23093    these cases, the line height does not really matter, except that it
 23094    must not be zero.  */
 23095 
 23096 static void
 23097 compute_line_metrics (struct it *it)
 23098 {
 23099   struct glyph_row *row = it->glyph_row;
 23100 
 23101   if (FRAME_WINDOW_P (it->f))
 23102     {
 23103       int i, min_y, max_y;
 23104 
 23105       /* The line may consist of one space only, that was added to
 23106          place the cursor on it.  If so, the row's height hasn't been
 23107          computed yet.  */
 23108       if (row->height == 0)
 23109         {
 23110           if (it->max_ascent + it->max_descent == 0)
 23111             it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
 23112           row->ascent = it->max_ascent;
 23113           row->height = it->max_ascent + it->max_descent;
 23114           row->phys_ascent = it->max_phys_ascent;
 23115           row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 23116           row->extra_line_spacing = it->max_extra_line_spacing;
 23117         }
 23118 
 23119       /* Compute the width of this line.  */
 23120       row->pixel_width = row->x;
 23121       for (i = 0; i < row->used[TEXT_AREA]; ++i)
 23122         row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
 23123 
 23124       eassert (row->pixel_width >= 0);
 23125       eassert (row->ascent >= 0 && row->height > 0);
 23126 
 23127       row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
 23128                             || MATRIX_ROW_OVERLAPS_PRED_P (row));
 23129 
 23130       /* If first line's physical ascent is larger than its logical
 23131          ascent, use the physical ascent, and make the row taller.
 23132          This makes accented characters fully visible.  */
 23133       if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
 23134           && row->phys_ascent > row->ascent)
 23135         {
 23136           row->height += row->phys_ascent - row->ascent;
 23137           row->ascent = row->phys_ascent;
 23138         }
 23139 
 23140       /* Compute how much of the line is visible.  */
 23141       row->visible_height = row->height;
 23142 
 23143       min_y = WINDOW_TAB_LINE_HEIGHT (it->w) + WINDOW_HEADER_LINE_HEIGHT (it->w);
 23144       max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
 23145 
 23146       if (row->y < min_y)
 23147         row->visible_height -= min_y - row->y;
 23148       if (row->y + row->height > max_y)
 23149         row->visible_height -= row->y + row->height - max_y;
 23150     }
 23151   else
 23152     {
 23153       row->pixel_width = row->used[TEXT_AREA];
 23154       if (row->continued_p)
 23155         row->pixel_width -= it->continuation_pixel_width;
 23156       else if (row->truncated_on_right_p)
 23157         row->pixel_width -= it->truncation_pixel_width;
 23158       row->ascent = row->phys_ascent = 0;
 23159       row->height = row->phys_height = row->visible_height = 1;
 23160       row->extra_line_spacing = 0;
 23161     }
 23162 
 23163   /* Compute a hash code for this row.  */
 23164   row->hash = row_hash (row);
 23165 
 23166   it->max_ascent = it->max_descent = 0;
 23167   it->max_phys_ascent = it->max_phys_descent = 0;
 23168 }
 23169 
 23170 
 23171 static void
 23172 clear_position (struct it *it)
 23173 {
 23174   it->position.charpos = 0;
 23175   it->position.bytepos = 0;
 23176 }
 23177 
 23178 /* Append one space to the glyph row of iterator IT if doing a
 23179    window-based redisplay.  The space has the same face as
 23180    IT->face_id.  Value is true if a space was added.
 23181 
 23182    This function is called to make sure that there is always one glyph
 23183    at the end of a glyph row that the cursor can be set on under
 23184    window-systems.  (If there weren't such a glyph we would not know
 23185    how wide and tall a box cursor should be displayed).
 23186 
 23187    At the same time this space let's a nicely handle clearing to the
 23188    end of the line if the row ends in italic text.  */
 23189 
 23190 static bool
 23191 append_space_for_newline (struct it *it, bool default_face_p)
 23192 {
 23193   int n = it->glyph_row->used[TEXT_AREA];
 23194 
 23195   if (it->glyph_row->glyphs[TEXT_AREA] + n
 23196       < it->glyph_row->glyphs[1 + TEXT_AREA])
 23197     {
 23198       /* Save some values that must not be changed.
 23199          Must save IT->c and IT->len because otherwise
 23200          ITERATOR_AT_END_P wouldn't work anymore after
 23201          append_space_for_newline has been called.  */
 23202       enum display_element_type saved_what = it->what;
 23203       int saved_c = it->c, saved_len = it->len;
 23204       int saved_char_to_display = it->char_to_display;
 23205       int saved_x = it->current_x;
 23206       const int saved_face_id = it->face_id;
 23207       bool saved_box_end = it->end_of_box_run_p;
 23208       struct text_pos saved_pos = it->position;
 23209       Lisp_Object saved_object = it->object;
 23210       struct face *face;
 23211 
 23212       it->what = IT_CHARACTER;
 23213       clear_position (it);
 23214       it->object = Qnil;
 23215       it->len = 1;
 23216 
 23217       int char_width = 1;
 23218 
 23219       if (default_face_p
 23220 #ifdef HAVE_WINDOW_SYSTEM
 23221           || FRAME_WINDOW_P (it->f)
 23222 #endif
 23223           )
 23224         {
 23225           const int local_default_face_id =
 23226             lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID);
 23227 
 23228 #ifdef HAVE_WINDOW_SYSTEM
 23229           if (FRAME_WINDOW_P (it->f))
 23230             {
 23231               struct face *default_face
 23232                 = FACE_FROM_ID (it->f, local_default_face_id);
 23233               struct font *font = (default_face->font
 23234                                    ? default_face->font
 23235                                    : FRAME_FONT (it->f));
 23236               char_width = (font->average_width
 23237                             ? font->average_width
 23238                             : font->space_width);
 23239             }
 23240 #endif
 23241           if (default_face_p)
 23242             it->face_id = local_default_face_id;
 23243         }
 23244       /* Corner case for when display-fill-column-indicator-mode
 23245          is active and the extra character should be added in the
 23246          same place than the line.  */
 23247 
 23248       const int indicator_column =
 23249         fill_column_indicator_column (it, char_width);
 23250       int saved_end_of_box_run = it->end_of_box_run_p;
 23251       bool should_keep_end_of_box_run = false;
 23252 
 23253       if (it->current_x == indicator_column)
 23254         {
 23255           it->c = it->char_to_display
 23256             = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23257           it->face_id
 23258             = merge_faces (it->w, Qfill_column_indicator,
 23259                            0, saved_face_id);
 23260           face = FACE_FROM_ID (it->f, it->face_id);
 23261         }
 23262       else
 23263         {
 23264           it->c = it->char_to_display = ' ';
 23265           /* If the default face was remapped, be sure to use the
 23266              remapped face for the appended newline.  */
 23267 
 23268           face = FACE_FROM_ID (it->f, it->face_id);
 23269           it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
 23270           /* In R2L rows, we will prepend a stretch glyph that will
 23271              have the end_of_box_run_p flag set for it, so there's no
 23272              need for the appended newline glyph to have that flag
 23273              set.  */
 23274           if (!(it->glyph_row->reversed_p
 23275                 /* But if the appended newline glyph goes all the way to
 23276                    the end of the row, there will be no stretch glyph,
 23277                    so leave the box flag set.  */
 23278                 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x))
 23279             should_keep_end_of_box_run = true;
 23280         }
 23281       PRODUCE_GLYPHS (it);
 23282       /* Restore the end_of_box_run_p flag which was reset by
 23283          PRODUCE_GLYPHS.  */
 23284       if (should_keep_end_of_box_run)
 23285         it->end_of_box_run_p = saved_end_of_box_run;
 23286 #ifdef HAVE_WINDOW_SYSTEM
 23287       if (FRAME_WINDOW_P (it->f))
 23288         {
 23289           /* Make sure this space glyph has the right ascent and
 23290              descent values, or else cursor at end of line will look
 23291              funny, and height of empty lines will be incorrect.  */
 23292           struct glyph *g = it->glyph_row->glyphs[TEXT_AREA] + n;
 23293           struct font *font = face->font ? face->font : FRAME_FONT (it->f);
 23294           if (n == 0)
 23295             {
 23296               Lisp_Object height, total_height;
 23297               int extra_line_spacing = it->extra_line_spacing;
 23298               int boff = font->baseline_offset;
 23299 
 23300               if (font->vertical_centering)
 23301                 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 23302 
 23303               it->object = saved_object; /* get_it_property needs this */
 23304               normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 23305               /* Must do a subset of line height processing from
 23306                  gui_produce_glyph for newline characters.  */
 23307               height = get_it_property (it, Qline_height);
 23308               if (CONSP (height)
 23309                   && CONSP (XCDR (height))
 23310                   && NILP (XCDR (XCDR (height))))
 23311                 {
 23312                   total_height = XCAR (XCDR (height));
 23313                   height = XCAR (height);
 23314                 }
 23315               else
 23316                 total_height = Qnil;
 23317               height = calc_line_height_property (it, height, font, boff, true);
 23318 
 23319               if (it->override_ascent >= 0)
 23320                 {
 23321                   it->ascent = it->override_ascent;
 23322                   it->descent = it->override_descent;
 23323                   boff = it->override_boff;
 23324                 }
 23325               if (EQ (height, Qt))
 23326                 extra_line_spacing = 0;
 23327               else
 23328                 {
 23329                   Lisp_Object spacing;
 23330 
 23331                   it->phys_ascent = it->ascent;
 23332                   it->phys_descent = it->descent;
 23333                   if (!NILP (height)
 23334                       && XFIXNUM (height) > it->ascent + it->descent)
 23335                     it->ascent = XFIXNUM (height) - it->descent;
 23336 
 23337                   if (!NILP (total_height))
 23338                     spacing = calc_line_height_property (it, total_height, font,
 23339                                                          boff, false);
 23340                   else
 23341                     {
 23342                       spacing = get_it_property (it, Qline_spacing);
 23343                       spacing = calc_line_height_property (it, spacing, font,
 23344                                                            boff, false);
 23345                     }
 23346                   if (FIXNUMP (spacing))
 23347                     {
 23348                       extra_line_spacing = XFIXNUM (spacing);
 23349                       if (!NILP (total_height))
 23350                         extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 23351                     }
 23352                 }
 23353               if (extra_line_spacing > 0)
 23354                 {
 23355                   it->descent += extra_line_spacing;
 23356                   if (extra_line_spacing > it->max_extra_line_spacing)
 23357                     it->max_extra_line_spacing = extra_line_spacing;
 23358                 }
 23359               it->max_ascent = it->ascent;
 23360               it->max_descent = it->descent;
 23361               /* Make sure compute_line_metrics recomputes the row height.  */
 23362               it->glyph_row->height = 0;
 23363             }
 23364 
 23365           g->ascent = it->max_ascent;
 23366           g->descent = it->max_descent;
 23367         }
 23368 #endif /* HAVE_WINDOW_SYSTEM  */
 23369       it->override_ascent = -1;
 23370       it->constrain_row_ascent_descent_p = false;
 23371       it->current_x = saved_x;
 23372       it->object = saved_object;
 23373       it->position = saved_pos;
 23374       it->what = saved_what;
 23375       it->face_id = saved_face_id;
 23376       it->len = saved_len;
 23377       it->c = saved_c;
 23378       it->char_to_display = saved_char_to_display;
 23379       it->end_of_box_run_p = saved_box_end;
 23380       return true;
 23381     }
 23382 
 23383   return false;
 23384 }
 23385 
 23386 
 23387 /* Extend the face of the last glyph in the text area of IT->glyph_row
 23388    to the end of the display line.  Called from display_line.  If the
 23389    glyph row is empty, add a space glyph to it so that we know the
 23390    face to draw.  Set the glyph row flag fill_line_p.  If the glyph
 23391    row is R2L, prepend a stretch glyph to cover the empty space to the
 23392    left of the leftmost glyph.  */
 23393 
 23394 static void
 23395 extend_face_to_end_of_line (struct it *it)
 23396 {
 23397   struct frame *f = it->f;
 23398 
 23399   /* If line is already filled, do nothing.  Non window-system frames
 23400      get a grace of one more ``pixel'' because their characters are
 23401      1-``pixel'' wide, so they hit the equality too early.  This grace
 23402      is needed only for R2L rows that are not continued, to produce
 23403      one extra blank where we could display the cursor.  */
 23404   if ((it->current_x >= it->last_visible_x
 23405        + (!FRAME_WINDOW_P (f)
 23406           && it->glyph_row->reversed_p
 23407           && !it->glyph_row->continued_p))
 23408       /* If the window has display margins, we will need to extend
 23409          their face even if the text area is filled.  */
 23410       && !(WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23411            || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0))
 23412     return;
 23413 
 23414   specpdl_ref count = SPECPDL_INDEX ();
 23415 
 23416   /* Don't allow the user to quit out of face-merging code, in case
 23417      this is called when redisplaying a non-selected window, with
 23418      point temporarily moved to window-point.  */
 23419   specbind (Qinhibit_quit, Qt);
 23420   /* The default face, possibly remapped. */
 23421   struct face *default_face =
 23422     FACE_FROM_ID_OR_NULL (f, lookup_basic_face (it->w, f, DEFAULT_FACE_ID));
 23423   if (!default_face)
 23424     return;
 23425 
 23426   const int extend_face_id =
 23427     (it->face_id == default_face->id || it->s != NULL)
 23428     ? it->face_id
 23429     : (it->glyph_row->ends_at_zv_p
 23430        ? default_face->id
 23431        : face_at_pos (it, LFACE_EXTEND_INDEX));
 23432   unbind_to (count, Qnil);
 23433 
 23434   /* Face extension extends the background and box of IT->extend_face_id
 23435      to the end of the line.  If the background equals the background
 23436      of the frame, we don't have to do anything.  */
 23437   struct face *face = FACE_FROM_ID (f, (it->face_before_selective_p
 23438                                         ? it->saved_face_id
 23439                                         : extend_face_id));
 23440 
 23441   if (FRAME_WINDOW_P (f)
 23442       && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
 23443       && face->box == FACE_NO_BOX
 23444       && face->underline == FACE_NO_UNDERLINE
 23445       && !face->overline_p
 23446       && !face->strike_through_p
 23447       && face->background == FRAME_BACKGROUND_PIXEL (f)
 23448 #ifdef HAVE_WINDOW_SYSTEM
 23449       && !face->stipple
 23450 #endif
 23451       && !it->glyph_row->reversed_p
 23452       && !display_fill_column_indicator)
 23453     return;
 23454 
 23455   /* Set the glyph row flag indicating that the face of the last glyph
 23456      in the text area has to be drawn to the end of the text area.  */
 23457   it->glyph_row->fill_line_p = true;
 23458 
 23459   const int orig_face_id = it->face_id;
 23460   /* If current character of IT is not ASCII, make sure we have the
 23461      ASCII face.  This will be automatically undone the next time
 23462      get_next_display_element returns a multibyte character.  Note
 23463      that the character will always be single byte in unibyte
 23464      text.  */
 23465   if (!ASCII_CHAR_P (it->c))
 23466     it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
 23467 
 23468 
 23469 #ifdef HAVE_WINDOW_SYSTEM
 23470   if (FRAME_WINDOW_P (f))
 23471     {
 23472       /* If the row is empty, add a space with the current face of IT,
 23473          so that we know which face to draw.  */
 23474       if (it->glyph_row->used[TEXT_AREA] == 0)
 23475         {
 23476           it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
 23477           it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
 23478           it->glyph_row->used[TEXT_AREA] = 1;
 23479         }
 23480       /* Mode line and the header line don't have margins, and
 23481          likewise the frame's tool-bar window, if there is any.  */
 23482       if (!(it->glyph_row->mode_line_p
 23483             || (WINDOWP (f->tab_bar_window)
 23484                 && it->w == XWINDOW (f->tab_bar_window))
 23485 #ifndef HAVE_EXT_TOOL_BAR
 23486             || (WINDOWP (f->tool_bar_window)
 23487                 && it->w == XWINDOW (f->tool_bar_window))
 23488 #endif
 23489             ))
 23490         {
 23491           if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23492               && it->glyph_row->used[LEFT_MARGIN_AREA] == 0)
 23493             {
 23494               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0] = space_glyph;
 23495               it->glyph_row->glyphs[LEFT_MARGIN_AREA][0].face_id =
 23496                 default_face->id;
 23497               it->glyph_row->used[LEFT_MARGIN_AREA] = 1;
 23498             }
 23499           if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23500               && it->glyph_row->used[RIGHT_MARGIN_AREA] == 0)
 23501             {
 23502               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0] = space_glyph;
 23503               it->glyph_row->glyphs[RIGHT_MARGIN_AREA][0].face_id =
 23504                 default_face->id;
 23505               it->glyph_row->used[RIGHT_MARGIN_AREA] = 1;
 23506             }
 23507 
 23508           struct font *font = (default_face->font
 23509                                ? default_face->font
 23510                                : FRAME_FONT (f));
 23511 
 23512           const int char_width = (font->average_width
 23513                                   ? font->average_width
 23514                                   : font->space_width);
 23515 
 23516           const int indicator_column =
 23517             fill_column_indicator_column (it, char_width);
 23518 
 23519           const char saved_char = it->char_to_display;
 23520           const struct text_pos saved_pos = it->position;
 23521           const bool saved_avoid_cursor = it->avoid_cursor_p;
 23522           const bool saved_box_start = it->start_of_box_run_p;
 23523           Lisp_Object save_object = it->object;
 23524           const int saved_face_id = it->face_id;
 23525 
 23526           it->face_id = extend_face_id;
 23527           it->avoid_cursor_p = true;
 23528           it->object = Qnil;
 23529 
 23530           const int stretch_height = it->ascent + it->descent;
 23531           const int stretch_ascent =
 23532             (stretch_height * FONT_BASE (font)) / FONT_HEIGHT (font);
 23533 
 23534           if (indicator_column >= 0
 23535               && indicator_column > it->current_x
 23536               && indicator_column < it->last_visible_x)
 23537             {
 23538 
 23539               /* Here we subtract char_width because we want the
 23540                  column indicator in the column INDICATOR_COLUMN,
 23541                  not after it.  */
 23542               const int stretch_width =
 23543                 indicator_column - it->current_x - char_width;
 23544 
 23545               clear_position (it);
 23546 
 23547               /* Only generate a stretch glyph if there is distance
 23548                  between current_x and the indicator position.  */
 23549               if (stretch_width > 0)
 23550                 {
 23551                   append_stretch_glyph (it, Qnil, stretch_width,
 23552                                         stretch_height, stretch_ascent);
 23553                 }
 23554 
 23555               /* Generate the glyph indicator only if
 23556                  append_space_for_newline didn't already.  */
 23557               if (it->current_x < indicator_column)
 23558                 {
 23559                   const int save_face_id = it->face_id;
 23560                   const int save_ascent = it->ascent;
 23561                   const int save_descent = it->descent;
 23562                   it->char_to_display
 23563                     = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23564                   it->face_id
 23565                     = merge_faces (it->w, Qfill_column_indicator,
 23566                                    0, extend_face_id);
 23567                   PRODUCE_GLYPHS (it);
 23568                   it->face_id = save_face_id;
 23569                   it->ascent = save_ascent;
 23570                   it->descent = save_descent;
 23571                 }
 23572             }
 23573 
 23574           /* Fill space until window edge with the merged face.  Do that
 23575              only for L2R rows, as R2L rows are handled specially below.  */
 23576           if (!it->glyph_row->reversed_p)
 23577             {
 23578               const int stretch_width = it->last_visible_x - it->current_x;
 23579 
 23580               if (stretch_width > 0)
 23581                 {
 23582                   clear_position (it);
 23583                   append_stretch_glyph (it, Qnil, stretch_width,
 23584                                         stretch_height, stretch_ascent);
 23585                 }
 23586             }
 23587 
 23588           it->char_to_display = saved_char;
 23589           it->position = saved_pos;
 23590           it->avoid_cursor_p = saved_avoid_cursor;
 23591           it->start_of_box_run_p = saved_box_start;
 23592           it->object = save_object;
 23593           it->face_id = saved_face_id;
 23594         }
 23595       if (it->glyph_row->reversed_p)
 23596         {
 23597           /* Prepend a stretch glyph to the row, such that the
 23598              rightmost glyph will be drawn flushed all the way to the
 23599              right margin of the window.  The stretch glyph that will
 23600              occupy the empty space, if any, to the left of the
 23601              glyph.  */
 23602           struct font *font = face->font ? face->font : FRAME_FONT (f);
 23603           struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
 23604           struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
 23605           struct glyph *g;
 23606           int row_width, stretch_ascent, stretch_width;
 23607           struct text_pos saved_pos;
 23608           int saved_face_id;
 23609           bool saved_avoid_cursor, saved_box_start;
 23610 
 23611           for (row_width = 0, g = row_start; g < row_end; g++)
 23612             row_width += g->pixel_width;
 23613 
 23614           /* FIXME: There are various minor display glitches in R2L
 23615              rows when only one of the fringes is missing.  The
 23616              strange condition below produces the least bad effect.  */
 23617           if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
 23618               == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
 23619               || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
 23620             stretch_width = window_box_width (it->w, TEXT_AREA);
 23621           else
 23622             stretch_width = it->last_visible_x - it->first_visible_x;
 23623           stretch_width -= row_width;
 23624 
 23625           if (stretch_width > 0)
 23626             {
 23627               stretch_ascent =
 23628                 (((it->ascent + it->descent)
 23629                   * FONT_BASE (font)) / FONT_HEIGHT (font));
 23630               saved_pos = it->position;
 23631               clear_position (it);
 23632               saved_avoid_cursor = it->avoid_cursor_p;
 23633               it->avoid_cursor_p = true;
 23634               saved_face_id = it->face_id;
 23635               saved_box_start = it->start_of_box_run_p;
 23636               /* The last row's stretch glyph should get the default
 23637                  face, to avoid painting the rest of the window with
 23638                  the region face, if the region ends at ZV.  */
 23639               it->face_id = (it->glyph_row->ends_at_zv_p ?
 23640                              default_face->id : face->id);
 23641 
 23642               it->start_of_box_run_p = false;
 23643               append_stretch_glyph (it, Qnil, stretch_width,
 23644                                     it->ascent + it->descent, stretch_ascent);
 23645               it->position = saved_pos;
 23646               it->avoid_cursor_p = saved_avoid_cursor;
 23647               it->face_id = saved_face_id;
 23648               it->start_of_box_run_p = saved_box_start;
 23649             }
 23650           /* If stretch_width comes out negative, it means that the
 23651              last glyph is only partially visible.  In R2L rows, we
 23652              want the leftmost glyph to be partially visible, so we
 23653              need to give the row the corresponding left offset.  */
 23654           if (stretch_width < 0)
 23655             it->glyph_row->x = stretch_width;
 23656         }
 23657       it->face_id = orig_face_id;
 23658     }
 23659   else
 23660 #endif  /* HAVE_WINDOW_SYSTEM */
 23661     {
 23662       /* Save some values that must not be changed.  */
 23663       int saved_x = it->current_x;
 23664       struct text_pos saved_pos = it->position;
 23665       Lisp_Object saved_object = it->object;;
 23666       enum display_element_type saved_what = it->what;
 23667 
 23668       it->what = IT_CHARACTER;
 23669       clear_position (it);
 23670       it->object = Qnil;
 23671       it->c = it->char_to_display = ' ';
 23672       it->len = 1;
 23673 
 23674       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 23675           && (it->glyph_row->used[LEFT_MARGIN_AREA]
 23676               < WINDOW_LEFT_MARGIN_WIDTH (it->w))
 23677           && !it->glyph_row->mode_line_p
 23678           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23679         {
 23680           struct glyph *g = it->glyph_row->glyphs[LEFT_MARGIN_AREA];
 23681           struct glyph *e = g + it->glyph_row->used[LEFT_MARGIN_AREA];
 23682 
 23683           for (it->current_x = 0; g < e; g++)
 23684             it->current_x += g->pixel_width;
 23685 
 23686           it->area = LEFT_MARGIN_AREA;
 23687           it->face_id = default_face->id;
 23688           while (it->glyph_row->used[LEFT_MARGIN_AREA]
 23689                  < WINDOW_LEFT_MARGIN_WIDTH (it->w)
 23690                  && g < it->glyph_row->glyphs[TEXT_AREA])
 23691             {
 23692               PRODUCE_GLYPHS (it);
 23693               /* term.c:produce_glyphs advances it->current_x only for
 23694                  TEXT_AREA.  */
 23695               it->current_x += it->pixel_width;
 23696               g++;
 23697             }
 23698 
 23699           it->current_x = saved_x;
 23700           it->area = TEXT_AREA;
 23701         }
 23702 
 23703       /* The last row's blank glyphs should get the default face, to
 23704          avoid painting the rest of the window with the region face,
 23705          if the region ends at ZV.  */
 23706       it->face_id = (it->glyph_row->ends_at_zv_p ?
 23707                      default_face->id : face->id);
 23708 
 23709       /* Make sure our idea of current_x is in sync with the glyphs
 23710          actually in the glyph row.  They might differ because
 23711          append_space_for_newline can insert one glyph without
 23712          updating current_x.  */
 23713       it->current_x = it->glyph_row->used[TEXT_AREA];
 23714 
 23715       /* The above assignment causes the code below to use a
 23716          non-standard semantics of it->current_x: it is measured
 23717          relative to the beginning of the text-area, thus disregarding
 23718          the window's hscroll.  That is why we need to correct the
 23719          indicator column for the hscroll, otherwise the indicator
 23720          will not move together with the text as result of horizontal
 23721          scrolling.  */
 23722       const int indicator_column =
 23723         fill_column_indicator_column (it, 1) - it->first_visible_x;
 23724 
 23725       /* Display fill-column indicator if needed.  */
 23726       while (it->current_x <= it->last_visible_x)
 23727         {
 23728           if (it->current_x != indicator_column)
 23729             PRODUCE_GLYPHS (it);
 23730           else
 23731             {
 23732               int saved_face_id = it->face_id;
 23733               it->face_id
 23734                 = merge_faces (it->w, Qfill_column_indicator, 0, extend_face_id);
 23735               it->c = it->char_to_display
 23736                 = XFIXNAT (Vdisplay_fill_column_indicator_character);
 23737 
 23738               PRODUCE_GLYPHS (it);
 23739 
 23740               it->face_id = saved_face_id;
 23741               it->c = it->char_to_display = ' ';
 23742             }
 23743         }
 23744 
 23745       if (WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0
 23746           && (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23747               < WINDOW_RIGHT_MARGIN_WIDTH (it->w))
 23748           && !it->glyph_row->mode_line_p
 23749           && face->background != FRAME_BACKGROUND_PIXEL (f))
 23750         {
 23751           struct glyph *g = it->glyph_row->glyphs[RIGHT_MARGIN_AREA];
 23752           struct glyph *e = g + it->glyph_row->used[RIGHT_MARGIN_AREA];
 23753 
 23754           for ( ; g < e; g++)
 23755             it->current_x += g->pixel_width;
 23756 
 23757           it->area = RIGHT_MARGIN_AREA;
 23758           it->face_id = default_face->id;
 23759           while (it->glyph_row->used[RIGHT_MARGIN_AREA]
 23760                  < WINDOW_RIGHT_MARGIN_WIDTH (it->w)
 23761                  && g < it->glyph_row->glyphs[LAST_AREA])
 23762             {
 23763               PRODUCE_GLYPHS (it);
 23764               it->current_x += it->pixel_width;
 23765               g++;
 23766             }
 23767 
 23768           it->area = TEXT_AREA;
 23769         }
 23770 
 23771       /* Don't count these blanks really.  It would let us insert a left
 23772          truncation glyph below and make us set the cursor on them, maybe.  */
 23773       it->current_x = saved_x;
 23774       it->object = saved_object;
 23775       it->position = saved_pos;
 23776       it->what = saved_what;
 23777       it->face_id = orig_face_id;
 23778     }
 23779 }
 23780 
 23781 
 23782 /* Value is true if text starting at CHARPOS in current_buffer is
 23783    trailing whitespace.  */
 23784 
 23785 static bool
 23786 trailing_whitespace_p (ptrdiff_t charpos)
 23787 {
 23788   ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
 23789   int c = 0;
 23790 
 23791   while (bytepos < ZV_BYTE
 23792          && (c = FETCH_BYTE (bytepos),
 23793              c == ' ' || c == '\t'))
 23794     ++bytepos;
 23795 
 23796   if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
 23797     {
 23798       if (bytepos != PT_BYTE)
 23799         return true;
 23800     }
 23801   return false;
 23802 }
 23803 
 23804 
 23805 /* Highlight trailing whitespace, if any, in row at IT.  */
 23806 
 23807 static void
 23808 highlight_trailing_whitespace (struct it *it)
 23809 {
 23810   struct glyph_row *row = it->glyph_row;
 23811   int used = row->used[TEXT_AREA];
 23812 
 23813   if (used)
 23814     {
 23815       struct glyph *start = row->glyphs[TEXT_AREA];
 23816       struct glyph *glyph = start + used - 1;
 23817 
 23818       if (row->reversed_p)
 23819         {
 23820           /* Right-to-left rows need to be processed in the opposite
 23821              direction, so swap the edge pointers. */
 23822           glyph = start;
 23823           start = row->glyphs[TEXT_AREA] + used - 1;
 23824         }
 23825 
 23826       /* Skip over glyphs inserted to display the cursor at the
 23827          end of a line, for extending the face of the last glyph
 23828          to the end of the line on terminals, and for truncation
 23829          and continuation glyphs.  */
 23830       if (!row->reversed_p)
 23831         {
 23832           while (glyph >= start
 23833                  && (glyph->type == CHAR_GLYPH
 23834                      || glyph->type == STRETCH_GLYPH)
 23835                  && NILP (glyph->object))
 23836             --glyph;
 23837         }
 23838       else
 23839         {
 23840           while (glyph <= start
 23841                  && (glyph->type == CHAR_GLYPH || glyph->type == STRETCH_GLYPH)
 23842                  && NILP (glyph->object))
 23843             ++glyph;
 23844         }
 23845 
 23846       /* If last glyph is a space or stretch, and it's trailing
 23847          whitespace, set the face of all trailing whitespace glyphs in
 23848          IT->glyph_row to `trailing-whitespace'.  */
 23849       if ((row->reversed_p ? glyph <= start : glyph >= start)
 23850           && BUFFERP (glyph->object)
 23851           && (glyph->type == STRETCH_GLYPH
 23852               || (glyph->type == CHAR_GLYPH
 23853                   && glyph->u.ch == ' '))
 23854           && trailing_whitespace_p (glyph->charpos))
 23855         {
 23856           int face_id = lookup_named_face (it->w, it->f, Qtrailing_whitespace, false);
 23857           if (face_id < 0)
 23858             return;
 23859 
 23860           if (!row->reversed_p)
 23861             {
 23862               while (glyph >= start
 23863                      && BUFFERP (glyph->object)
 23864                      && (glyph->type == STRETCH_GLYPH
 23865                          || (glyph->type == CHAR_GLYPH
 23866                              && glyph->u.ch == ' ')))
 23867                 (glyph--)->face_id = face_id;
 23868             }
 23869           else
 23870             {
 23871               while (glyph <= start
 23872                      && BUFFERP (glyph->object)
 23873                      && (glyph->type == STRETCH_GLYPH
 23874                          || (glyph->type == CHAR_GLYPH
 23875                              && glyph->u.ch == ' ')))
 23876                 (glyph++)->face_id = face_id;
 23877             }
 23878         }
 23879     }
 23880 }
 23881 
 23882 
 23883 /* Value is true if glyph row ROW should be
 23884    considered to hold the buffer position CHARPOS.  */
 23885 
 23886 static bool
 23887 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
 23888 {
 23889   bool result = true;
 23890 
 23891   if (charpos == CHARPOS (row->end.pos)
 23892       || charpos == MATRIX_ROW_END_CHARPOS (row))
 23893     {
 23894       /* Suppose the row ends on a string.
 23895          Unless the row is continued, that means it ends on a newline
 23896          in the string.  If it's anything other than a display string
 23897          (e.g., a before-string from an overlay), we don't want the
 23898          cursor there.  (This heuristic seems to give the optimal
 23899          behavior for the various types of multi-line strings.)
 23900          One exception: if the string has `cursor' property on one of
 23901          its characters, we _do_ want the cursor there.  */
 23902       if (CHARPOS (row->end.string_pos) >= 0)
 23903         {
 23904           if (row->continued_p)
 23905             result = true;
 23906           else
 23907             {
 23908               /* Check for `display' property.  */
 23909               struct glyph *beg = row->glyphs[TEXT_AREA];
 23910               struct glyph *end = beg + row->used[TEXT_AREA] - 1;
 23911               struct glyph *glyph;
 23912 
 23913               result = false;
 23914               for (glyph = end; glyph >= beg; --glyph)
 23915                 if (STRINGP (glyph->object))
 23916                   {
 23917                     Lisp_Object prop
 23918                       = Fget_char_property (make_fixnum (charpos),
 23919                                             Qdisplay, Qnil);
 23920                     result =
 23921                       (!NILP (prop)
 23922                        && display_prop_string_p (prop, glyph->object));
 23923                     /* If there's a `cursor' property on one of the
 23924                        string's characters, this row is a cursor row,
 23925                        even though this is not a display string.  */
 23926                     if (!result)
 23927                       {
 23928                         Lisp_Object s = glyph->object;
 23929 
 23930                         for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
 23931                           {
 23932                             ptrdiff_t gpos = glyph->charpos;
 23933 
 23934                             if (!NILP (Fget_char_property (make_fixnum (gpos),
 23935                                                            Qcursor, s)))
 23936                               {
 23937                                 result = true;
 23938                                 break;
 23939                               }
 23940                           }
 23941                       }
 23942                     break;
 23943                   }
 23944             }
 23945         }
 23946       else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
 23947         {
 23948           /* If the row ends in middle of a real character,
 23949              and the line is continued, we want the cursor here.
 23950              That's because CHARPOS (ROW->end.pos) would equal
 23951              PT if PT is before the character.  */
 23952           if (!row->ends_in_ellipsis_p)
 23953             result = row->continued_p;
 23954           else
 23955           /* If the row ends in an ellipsis, then
 23956              CHARPOS (ROW->end.pos) will equal point after the
 23957              invisible text.  We want that position to be displayed
 23958              after the ellipsis.  */
 23959             result = false;
 23960         }
 23961       /* If the row ends at ZV, display the cursor at the end of that
 23962          row instead of at the start of the row below.  */
 23963       else
 23964         result = row->ends_at_zv_p;
 23965     }
 23966 
 23967   return result;
 23968 }
 23969 
 23970 /* Value is true if glyph row ROW should be
 23971    used to hold the cursor.  */
 23972 
 23973 static bool
 23974 cursor_row_p (struct glyph_row *row)
 23975 {
 23976   return row_for_charpos_p (row, PT);
 23977 }
 23978 
 23979 
 23980 
 23981 /* Push the property PROP so that it will be rendered at the current
 23982    position in IT.  Return true if PROP was successfully pushed, false
 23983    otherwise.  Called from handle_line_prefix to handle the
 23984    `line-prefix' and `wrap-prefix' properties.  */
 23985 
 23986 static bool
 23987 push_prefix_prop (struct it *it, Lisp_Object prop)
 23988 {
 23989   struct text_pos pos =
 23990     STRINGP (it->string) ? it->current.string_pos : it->current.pos;
 23991 
 23992   eassert (it->method == GET_FROM_BUFFER
 23993            || it->method == GET_FROM_DISPLAY_VECTOR
 23994            || it->method == GET_FROM_STRING
 23995            || it->method == GET_FROM_IMAGE);
 23996 
 23997   /* We need to save the current buffer/string position, so it will be
 23998      restored by pop_it, because iterate_out_of_display_property
 23999      depends on that being set correctly, but some situations leave
 24000      it->position not yet set when this function is called.  */
 24001   push_it (it, &pos);
 24002 
 24003   if (STRINGP (prop))
 24004     {
 24005       if (SCHARS (prop) == 0)
 24006         {
 24007           pop_it (it);
 24008           return false;
 24009         }
 24010 
 24011       it->string = prop;
 24012       it->string_from_prefix_prop_p = true;
 24013       it->multibyte_p = STRING_MULTIBYTE (it->string);
 24014       it->current.overlay_string_index = -1;
 24015       IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
 24016       it->end_charpos = it->string_nchars = SCHARS (it->string);
 24017       it->method = GET_FROM_STRING;
 24018       it->stop_charpos = 0;
 24019       it->prev_stop = 0;
 24020       it->base_level_stop = 0;
 24021       it->cmp_it.id = -1;
 24022 
 24023       /* Force paragraph direction to be that of the parent
 24024          buffer/string.  */
 24025       if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
 24026         it->paragraph_embedding = it->bidi_it.paragraph_dir;
 24027       else
 24028         it->paragraph_embedding = L2R;
 24029 
 24030       /* Set up the bidi iterator for this display string.  */
 24031       if (it->bidi_p)
 24032         {
 24033           it->bidi_it.string.lstring = it->string;
 24034           it->bidi_it.string.s = NULL;
 24035           it->bidi_it.string.schars = it->end_charpos;
 24036           it->bidi_it.string.bufpos = IT_CHARPOS (*it);
 24037           it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
 24038           it->bidi_it.string.unibyte = !it->multibyte_p;
 24039           it->bidi_it.w = it->w;
 24040           bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
 24041         }
 24042     }
 24043   else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
 24044     {
 24045       it->method = GET_FROM_STRETCH;
 24046       it->object = prop;
 24047     }
 24048 #ifdef HAVE_WINDOW_SYSTEM
 24049   else if (IMAGEP (prop))
 24050     {
 24051       it->what = IT_IMAGE;
 24052       it->image_id = lookup_image (it->f, prop, it->face_id);
 24053       it->method = GET_FROM_IMAGE;
 24054     }
 24055 #endif /* HAVE_WINDOW_SYSTEM */
 24056   else
 24057     {
 24058       pop_it (it);              /* bogus display property, give up */
 24059       return false;
 24060     }
 24061 
 24062   return true;
 24063 }
 24064 
 24065 /* Return the character-property PROP at the current position in IT.  */
 24066 
 24067 static Lisp_Object
 24068 get_it_property (struct it *it, Lisp_Object prop)
 24069 {
 24070   Lisp_Object position, object = it->object;
 24071 
 24072   if (STRINGP (object))
 24073     position = make_fixnum (IT_STRING_CHARPOS (*it));
 24074   else if (BUFFERP (object))
 24075     {
 24076       position = make_fixnum (IT_CHARPOS (*it));
 24077       object = it->window;
 24078     }
 24079   else
 24080     return Qnil;
 24081 
 24082   return Fget_char_property (position, prop, object);
 24083 }
 24084 
 24085 /* Return the line-prefix/wrap-prefix property, checking both the
 24086    current IT->OBJECT and the underlying buffer text.  */
 24087 
 24088 static Lisp_Object
 24089 get_line_prefix_it_property (struct it *it, Lisp_Object prop)
 24090 {
 24091   Lisp_Object prefix = get_it_property (it, prop);
 24092 
 24093   /* If we are looking at a display or overlay string, check also the
 24094      underlying buffer text.  */
 24095   if (NILP (prefix) && it->sp > 0 && STRINGP (it->object))
 24096     return Fget_char_property (make_fixnum (IT_CHARPOS (*it)), prop,
 24097                                it->w->contents);
 24098   return prefix;
 24099 }
 24100 
 24101 /* See if there's a line- or wrap-prefix, and if so, push it on IT.  */
 24102 
 24103 static void
 24104 handle_line_prefix (struct it *it)
 24105 {
 24106   Lisp_Object prefix;
 24107 
 24108   if (it->continuation_lines_width > 0)
 24109     {
 24110       prefix = get_line_prefix_it_property (it, Qwrap_prefix);
 24111       if (NILP (prefix))
 24112         prefix = Vwrap_prefix;
 24113     }
 24114   else
 24115     {
 24116       prefix = get_line_prefix_it_property (it, Qline_prefix);
 24117       if (NILP (prefix))
 24118         prefix = Vline_prefix;
 24119     }
 24120   if (! NILP (prefix) && push_prefix_prop (it, prefix))
 24121     {
 24122       /* If the prefix is wider than the window, and we try to wrap
 24123          it, it would acquire its own wrap prefix, and so on till the
 24124          iterator stack overflows.  So, don't wrap the prefix.  */
 24125       it->line_wrap = TRUNCATE;
 24126       it->avoid_cursor_p = true;
 24127     }
 24128 }
 24129 
 24130 
 24131 
 24132 /* Remove N glyphs at the start of a reversed IT->glyph_row.  Called
 24133    only for R2L lines from display_line and display_string, when they
 24134    decide that too many glyphs were produced by PRODUCE_GLYPHS, and
 24135    the line/string needs to be continued on the next glyph row.  */
 24136 static void
 24137 unproduce_glyphs (struct it *it, int n)
 24138 {
 24139   struct glyph *glyph, *end;
 24140 
 24141   eassert (it->glyph_row);
 24142   eassert (it->glyph_row->reversed_p);
 24143   eassert (it->area == TEXT_AREA);
 24144   eassert (n <= it->glyph_row->used[TEXT_AREA]);
 24145 
 24146   if (n > it->glyph_row->used[TEXT_AREA])
 24147     n = it->glyph_row->used[TEXT_AREA];
 24148   glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
 24149   end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
 24150   for ( ; glyph < end; glyph++)
 24151     glyph[-n] = *glyph;
 24152 }
 24153 
 24154 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
 24155    and ROW->maxpos.  */
 24156 static void
 24157 find_row_edges (struct it *it, struct glyph_row *row,
 24158                 ptrdiff_t min_pos, ptrdiff_t min_bpos,
 24159                 ptrdiff_t max_pos, ptrdiff_t max_bpos)
 24160 {
 24161   /* FIXME: Revisit this when glyph ``spilling'' in continuation
 24162      lines' rows is implemented for bidi-reordered rows.  */
 24163 
 24164   /* ROW->minpos is the value of min_pos, the minimal buffer position
 24165      we have in ROW, or ROW->start.pos if that is smaller.  */
 24166   if (min_pos <= ZV && min_pos < row->start.pos.charpos)
 24167     SET_TEXT_POS (row->minpos, min_pos, min_bpos);
 24168   else
 24169     /* We didn't find buffer positions smaller than ROW->start, or
 24170        didn't find _any_ valid buffer positions in any of the glyphs,
 24171        so we must trust the iterator's computed positions.  */
 24172       row->minpos = row->start.pos;
 24173   if (max_pos <= 0)
 24174     {
 24175       max_pos = CHARPOS (it->current.pos);
 24176       max_bpos = BYTEPOS (it->current.pos);
 24177     }
 24178 
 24179   /* Here are the various use-cases for ending the row, and the
 24180      corresponding values for ROW->maxpos:
 24181 
 24182      Line ends in a newline from buffer       eol_pos + 1
 24183      Line is continued from buffer            max_pos + 1
 24184      Line is truncated on right               it->current.pos
 24185      Line ends in a newline from string       max_pos + 1(*)
 24186       (*) + 1 only when line ends in a forward scan
 24187      Line is continued from string            max_pos
 24188      Line is continued from display vector    max_pos
 24189      Line is entirely from a string           min_pos == max_pos
 24190      Line is entirely from a display vector   min_pos == max_pos
 24191      Line that ends at ZV                     ZV
 24192 
 24193      If you discover other use-cases, please add them here as
 24194      appropriate.  */
 24195   if (row->ends_at_zv_p)
 24196     row->maxpos = it->current.pos;
 24197   else if (row->used[TEXT_AREA])
 24198     {
 24199       bool seen_this_string = false;
 24200       struct glyph_row *r1 = row - 1;
 24201 
 24202       /* Did we see the same display string on the previous row?  */
 24203       if (STRINGP (it->object)
 24204           /* this is not the first row */
 24205           && row > it->w->desired_matrix->rows
 24206           /* previous row is not the header line or tab-line */
 24207           && !r1->mode_line_p
 24208           /* previous row also ends in a newline from a string */
 24209           && r1->ends_in_newline_from_string_p)
 24210         {
 24211           struct glyph *start, *end;
 24212 
 24213           /* Search for the last glyph of the previous row that came
 24214              from buffer or string.  Depending on whether the row is
 24215              L2R or R2L, we need to process it front to back or the
 24216              other way round.  */
 24217           if (!r1->reversed_p)
 24218             {
 24219               start = r1->glyphs[TEXT_AREA];
 24220               end = start + r1->used[TEXT_AREA];
 24221               /* Glyphs inserted by redisplay have nil as their object.  */
 24222               while (end > start
 24223                      && NILP ((end - 1)->object)
 24224                      && (end - 1)->charpos <= 0)
 24225                 --end;
 24226               if (end > start)
 24227                 {
 24228                   if (EQ ((end - 1)->object, it->object))
 24229                     seen_this_string = true;
 24230                 }
 24231               else
 24232                 /* If all the glyphs of the previous row were inserted
 24233                    by redisplay, it means the previous row was
 24234                    produced from a single newline, which is only
 24235                    possible if that newline came from the same string
 24236                    as the one which produced this ROW.  */
 24237                 seen_this_string = true;
 24238             }
 24239           else
 24240             {
 24241               end = r1->glyphs[TEXT_AREA] - 1;
 24242               start = end + r1->used[TEXT_AREA];
 24243               while (end < start
 24244                      && NILP ((end + 1)->object)
 24245                      && (end + 1)->charpos <= 0)
 24246                 ++end;
 24247               if (end < start)
 24248                 {
 24249                   if (EQ ((end + 1)->object, it->object))
 24250                     seen_this_string = true;
 24251                 }
 24252               else
 24253                 seen_this_string = true;
 24254             }
 24255         }
 24256       /* Take note of each display string that covers a newline only
 24257          once, the first time we see it.  This is for when a display
 24258          string includes more than one newline in it.  */
 24259       if (row->ends_in_newline_from_string_p && !seen_this_string)
 24260         {
 24261           /* If we were scanning the buffer forward when we displayed
 24262              the string, we want to account for at least one buffer
 24263              position that belongs to this row (position covered by
 24264              the display string), so that cursor positioning will
 24265              consider this row as a candidate when point is at the end
 24266              of the visual line represented by this row.  This is not
 24267              required when scanning back, because max_pos will already
 24268              have a much larger value.  */
 24269           if (CHARPOS (row->end.pos) > max_pos)
 24270             inc_both (&max_pos, &max_bpos);
 24271           SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24272         }
 24273       else if (CHARPOS (it->eol_pos) > 0)
 24274         SET_TEXT_POS (row->maxpos,
 24275                       CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
 24276       else if (row->continued_p)
 24277         {
 24278           /* If max_pos is different from IT's current position, it
 24279              means IT->method does not belong to the display element
 24280              at max_pos.  However, it also means that the display
 24281              element at max_pos was displayed in its entirety on this
 24282              line, which is equivalent to saying that the next line
 24283              starts at the next buffer position.  */
 24284           if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
 24285             SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24286           else
 24287             {
 24288               inc_both (&max_pos, &max_bpos);
 24289               SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
 24290             }
 24291         }
 24292       else if (row->truncated_on_right_p)
 24293         /* display_line already called reseat_at_next_visible_line_start,
 24294            which puts the iterator at the beginning of the next line, in
 24295            the logical order. */
 24296         row->maxpos = it->current.pos;
 24297       else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
 24298         /* A line that is entirely from a string/image/stretch...  */
 24299         row->maxpos = row->minpos;
 24300       else
 24301         emacs_abort ();
 24302     }
 24303   else
 24304     row->maxpos = it->current.pos;
 24305 }
 24306 
 24307 /* Like display_count_lines, but capable of counting outside of the
 24308    current narrowed region.  */
 24309 static ptrdiff_t
 24310 display_count_lines_logically (ptrdiff_t start_byte, ptrdiff_t limit_byte,
 24311                                ptrdiff_t count, ptrdiff_t *byte_pos_ptr)
 24312 {
 24313   if (!display_line_numbers_widen || (BEGV == BEG && ZV == Z))
 24314     return display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24315 
 24316   ptrdiff_t val;
 24317   specpdl_ref pdl_count = SPECPDL_INDEX ();
 24318   record_unwind_protect (save_restriction_restore, save_restriction_save ());
 24319   labeled_restrictions_remove_in_current_buffer ();
 24320   Fwiden ();
 24321   val = display_count_lines (start_byte, limit_byte, count, byte_pos_ptr);
 24322   unbind_to (pdl_count, Qnil);
 24323   return val;
 24324 }
 24325 
 24326 /* Count the number of screen lines in window IT->w between character
 24327    position IT_CHARPOS(*IT) and the line showing that window's point.  */
 24328 static ptrdiff_t
 24329 display_count_lines_visually (struct it *it)
 24330 {
 24331   struct it tem_it;
 24332   ptrdiff_t to;
 24333   struct text_pos from;
 24334 
 24335   /* If we already calculated a relative line number, use that.  This
 24336      trick relies on the fact that visual lines (a.k.a. "glyph rows")
 24337      are laid out sequentially, one by one, for each sequence of calls
 24338      to display_line or other similar function that follows a call to
 24339      init_iterator.  */
 24340   if (it->lnum_bytepos > 0)
 24341     return it->lnum + 1;
 24342   else
 24343     {
 24344       specpdl_ref count = SPECPDL_INDEX ();
 24345 
 24346       if (IT_CHARPOS (*it) <= PT)
 24347         {
 24348           from = it->current.pos;
 24349           to = PT;
 24350         }
 24351       else
 24352         {
 24353           SET_TEXT_POS (from, PT, PT_BYTE);
 24354           to = IT_CHARPOS (*it);
 24355         }
 24356       /* Need to disable visual mode temporarily, since otherwise the
 24357          call to move_it_to below and inside start_display will cause
 24358          infinite recursion.  */
 24359       specbind (Qdisplay_line_numbers, Qrelative);
 24360       start_display (&tem_it, it->w, from);
 24361       /* Some redisplay optimizations could invoke us very far from
 24362          PT, which will make the caller painfully slow.  There should
 24363          be no need to go too far beyond the window's bottom, as any
 24364          such optimization will fail to show point anyway.  */
 24365       move_it_to (&tem_it, to, -1,
 24366                   tem_it.last_visible_y
 24367                   + (SCROLL_LIMIT + 10) * FRAME_LINE_HEIGHT (tem_it.f),
 24368                   -1, MOVE_TO_POS | MOVE_TO_Y);
 24369       unbind_to (count, Qnil);
 24370       return IT_CHARPOS (*it) <= PT ? -tem_it.vpos : tem_it.vpos;
 24371     }
 24372 }
 24373 
 24374 /* Produce the line-number glyphs for the current glyph_row.  If
 24375    IT->glyph_row is non-NULL, populate the row with the produced
 24376    glyphs.  */
 24377 static void
 24378 maybe_produce_line_number (struct it *it)
 24379 {
 24380   ptrdiff_t last_line = it->lnum;
 24381   ptrdiff_t start_from, bytepos;
 24382   ptrdiff_t this_line;
 24383   bool first_time = false;
 24384   ptrdiff_t beg_byte;
 24385   ptrdiff_t z_byte;
 24386   bool line_numbers_wide;
 24387   void *itdata = bidi_shelve_cache ();
 24388 
 24389   if (display_line_numbers_offset
 24390       && !display_line_numbers_widen
 24391       && !EQ (Vdisplay_line_numbers, Qvisual)
 24392       && !EQ (Vdisplay_line_numbers, Qrelative))
 24393     line_numbers_wide = true;
 24394   else
 24395     line_numbers_wide = display_line_numbers_widen;
 24396 
 24397   beg_byte = line_numbers_wide ? BEG_BYTE : BEGV_BYTE;
 24398   z_byte = line_numbers_wide ? Z_BYTE : ZV_BYTE;
 24399 
 24400   if (EQ (Vdisplay_line_numbers, Qvisual))
 24401     this_line = display_count_lines_visually (it);
 24402   else
 24403     {
 24404       if (!last_line)
 24405         {
 24406           /* If possible, reuse data cached by line-number-mode.  */
 24407           if (it->w->base_line_number > 0
 24408               && it->w->base_line_pos > 0
 24409               && it->w->base_line_pos <= IT_CHARPOS (*it)
 24410               /* line-number-mode always displays narrowed line
 24411                  numbers, so we cannot use its data if the user wants
 24412                  line numbers that disregard narrowing, or if the
 24413                  buffer's narrowing has just changed.  */
 24414               && !(line_numbers_wide
 24415                    && (BEG_BYTE != BEGV_BYTE || Z_BYTE != ZV_BYTE))
 24416               && !current_buffer->clip_changed)
 24417             {
 24418               start_from = CHAR_TO_BYTE (it->w->base_line_pos);
 24419               last_line = it->w->base_line_number - 1;
 24420             }
 24421           else
 24422             start_from = beg_byte;
 24423           if (!it->lnum_bytepos)
 24424             first_time = true;
 24425         }
 24426       else
 24427         start_from = it->lnum_bytepos;
 24428 
 24429       /* Paranoia: what if someone changes the narrowing since the
 24430          last time display_line was called?  Shouldn't really happen,
 24431          but who knows what some crazy Lisp invoked by :eval could do?  */
 24432       if (!(beg_byte <= start_from && start_from <= z_byte))
 24433         {
 24434           last_line = 0;
 24435           start_from = beg_byte;
 24436         }
 24437 
 24438       this_line =
 24439         last_line + display_count_lines_logically (start_from,
 24440                                                    IT_BYTEPOS (*it),
 24441                                                    IT_CHARPOS (*it), &bytepos);
 24442       eassert (this_line > 0 || (this_line == 0 && start_from == beg_byte));
 24443       eassert (bytepos == IT_BYTEPOS (*it));
 24444     }
 24445 
 24446   /* Record the line number information.  */
 24447   if (this_line != last_line || !it->lnum_bytepos)
 24448     {
 24449       it->lnum = this_line;
 24450       it->lnum_bytepos = IT_BYTEPOS (*it);
 24451     }
 24452 
 24453   /* Produce the glyphs for the line number.  */
 24454   struct it tem_it;
 24455   char lnum_buf[INT_STRLEN_BOUND (ptrdiff_t) + 1];
 24456   bool beyond_zv = IT_BYTEPOS (*it) >= ZV_BYTE ? true : false;
 24457   ptrdiff_t lnum_offset = -1; /* to produce 1-based line numbers */
 24458   int lnum_face_id = merge_faces (it->w, Qline_number, 0, DEFAULT_FACE_ID);
 24459   int current_lnum_face_id
 24460     = merge_faces (it->w, Qline_number_current_line, 0, DEFAULT_FACE_ID);
 24461   /* From here onwards, we must prevent freeing realized faces, because
 24462      we are using the above 2 face IDs for the glyphs we produce.  */
 24463   bool save_free_realized_faces = inhibit_free_realized_faces;
 24464   inhibit_free_realized_faces = true;
 24465   /* Compute point's line number if needed.  */
 24466   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24467        || EQ (Vdisplay_line_numbers, Qvisual)
 24468        || lnum_face_id != current_lnum_face_id)
 24469       && !it->pt_lnum)
 24470     {
 24471       ptrdiff_t ignored;
 24472       if (PT_BYTE > it->lnum_bytepos && !EQ (Vdisplay_line_numbers, Qvisual))
 24473         it->pt_lnum =
 24474           this_line + display_count_lines_logically (it->lnum_bytepos, PT_BYTE,
 24475                                                      PT, &ignored);
 24476       else
 24477         it->pt_lnum = display_count_lines_logically (beg_byte, PT_BYTE, PT,
 24478                                                      &ignored);
 24479     }
 24480   /* Compute the required width if needed.  */
 24481   if (!it->lnum_width)
 24482     {
 24483       if (FIXNATP (Vdisplay_line_numbers_width))
 24484         it->lnum_width = XFIXNAT (Vdisplay_line_numbers_width);
 24485 
 24486       /* Max line number to be displayed cannot be more than the one
 24487          corresponding to the last row of the desired matrix.  */
 24488       ptrdiff_t max_lnum;
 24489 
 24490       if (NILP (Vdisplay_line_numbers_current_absolute)
 24491           && (EQ (Vdisplay_line_numbers, Qrelative)
 24492               || EQ (Vdisplay_line_numbers, Qvisual)))
 24493         /* We subtract one more because the current line is always
 24494            zero in this mode.  */
 24495         max_lnum = it->w->desired_matrix->nrows - 2;
 24496       else if (EQ (Vdisplay_line_numbers, Qvisual))
 24497         max_lnum = it->pt_lnum + it->w->desired_matrix->nrows - 1;
 24498       else
 24499         max_lnum = this_line + it->w->desired_matrix->nrows - 1 - it->vpos;
 24500       max_lnum = max (1, max_lnum);
 24501       it->lnum_width = max (it->lnum_width, log10 (max_lnum) + 1);
 24502       eassert (it->lnum_width > 0);
 24503     }
 24504   if (EQ (Vdisplay_line_numbers, Qrelative))
 24505     lnum_offset = it->pt_lnum;
 24506   else if (EQ (Vdisplay_line_numbers, Qvisual))
 24507     lnum_offset = 0;
 24508   else if (display_line_numbers_offset)
 24509     lnum_offset -= display_line_numbers_offset;
 24510 
 24511   /* Under 'relative', display the absolute line number for the
 24512      current line, unless the user requests otherwise.  */
 24513   ptrdiff_t lnum_to_display = eabs (this_line - lnum_offset);
 24514   if ((EQ (Vdisplay_line_numbers, Qrelative)
 24515        || EQ (Vdisplay_line_numbers, Qvisual))
 24516       && lnum_to_display == 0
 24517       && !NILP (Vdisplay_line_numbers_current_absolute))
 24518     lnum_to_display = it->pt_lnum + 1;
 24519   /* In L2R rows we need to append the blank separator, in R2L
 24520      rows we need to prepend it.  But this function is usually
 24521      called when no display elements were produced from the
 24522      following line, so the paragraph direction might be unknown.
 24523      Therefore we cheat and add 2 blanks, one on either side.  */
 24524   pint2str (lnum_buf, it->lnum_width + 1, lnum_to_display);
 24525   strcat (lnum_buf, " ");
 24526 
 24527   /* Setup for producing the glyphs.  */
 24528   init_iterator (&tem_it, it->w, -1, -1, &scratch_glyph_row,
 24529                  /* FIXME: Use specialized face.  */
 24530                  DEFAULT_FACE_ID);
 24531   scratch_glyph_row.reversed_p = false;
 24532   scratch_glyph_row.used[TEXT_AREA] = 0;
 24533   SET_TEXT_POS (tem_it.position, 0, 0);
 24534   tem_it.avoid_cursor_p = true;
 24535   tem_it.bidi_p = true;
 24536   tem_it.bidi_it.type = WEAK_EN;
 24537   /* According to UAX#9, EN goes up 2 levels in L2R paragraph and
 24538      1 level in R2L paragraphs.  Emulate that, assuming we are in
 24539      an L2R paragraph.  */
 24540   tem_it.bidi_it.resolved_level = 2;
 24541 
 24542   /* We must leave space for 2 glyphs for continuation and truncation,
 24543      and at least one glyph for buffer text.  */
 24544   int width_limit =
 24545     tem_it.last_visible_x - tem_it.first_visible_x
 24546     - 3 * FRAME_COLUMN_WIDTH (it->f);
 24547 
 24548   tem_it.face_id = lnum_face_id;
 24549   /* Avoid displaying any face other than line-number on
 24550      empty lines beyond EOB.  */
 24551   if (lnum_face_id != current_lnum_face_id
 24552       && (EQ (Vdisplay_line_numbers, Qvisual)
 24553           ? this_line == 0
 24554           : this_line == it->pt_lnum)
 24555       && it->what != IT_EOB)
 24556     tem_it.face_id = current_lnum_face_id;
 24557   else if (!beyond_zv)
 24558     {
 24559       if (display_line_numbers_major_tick > 0
 24560           && (lnum_to_display % display_line_numbers_major_tick == 0))
 24561         tem_it.face_id = merge_faces (it->w, Qline_number_major_tick,
 24562                                       0, DEFAULT_FACE_ID);
 24563       else if (display_line_numbers_minor_tick > 0
 24564                && (lnum_to_display % display_line_numbers_minor_tick == 0))
 24565         tem_it.face_id = merge_faces (it->w, Qline_number_minor_tick,
 24566                                       0, DEFAULT_FACE_ID);
 24567     }
 24568 
 24569   /* Produce glyphs for the line number in a scratch glyph_row.  */
 24570   for (const char *p = lnum_buf; *p; p++)
 24571     {
 24572       /* For continuation lines and lines after ZV, instead of a line
 24573          number, produce a blank prefix of the same width.  */
 24574       if (beyond_zv
 24575           /* Don't display the same line number more than once.  */
 24576           || (!EQ (Vdisplay_line_numbers, Qvisual)
 24577               && (it->continuation_lines_width > 0
 24578                   || (this_line == last_line && !first_time))))
 24579         tem_it.c = tem_it.char_to_display = ' ';
 24580       else
 24581         tem_it.c = tem_it.char_to_display = *p;
 24582       tem_it.len = 1;
 24583       /* Make sure these glyphs will have a "position" of -1.  */
 24584       SET_TEXT_POS (tem_it.position, -1, -1);
 24585       PRODUCE_GLYPHS (&tem_it);
 24586 
 24587       /* Stop producing glyphs, and refrain from producing the line
 24588          number, if we don't have enough space on this line.  */
 24589       if (tem_it.current_x >= width_limit)
 24590         {
 24591           it->lnum_width = 0;
 24592           it->lnum_pixel_width = 0;
 24593           bidi_unshelve_cache (itdata, false);
 24594           inhibit_free_realized_faces = save_free_realized_faces;
 24595           return;
 24596         }
 24597     }
 24598 
 24599   inhibit_free_realized_faces = save_free_realized_faces;
 24600 
 24601   /* Record the width in pixels we need for the line number display.  */
 24602   it->lnum_pixel_width = tem_it.current_x;
 24603   /* Copy the produced glyphs into IT's glyph_row.  */
 24604   struct glyph *g = scratch_glyph_row.glyphs[TEXT_AREA];
 24605   struct glyph *e = g + scratch_glyph_row.used[TEXT_AREA];
 24606   struct glyph *p = it->glyph_row ? it->glyph_row->glyphs[TEXT_AREA] : NULL;
 24607   short *u = it->glyph_row ? &it->glyph_row->used[TEXT_AREA] : NULL;
 24608 
 24609   eassert (it->glyph_row == NULL || it->glyph_row->used[TEXT_AREA] == 0);
 24610 
 24611   for ( ; g < e; g++)
 24612     {
 24613       it->current_x += g->pixel_width;
 24614       /* The following is important when this function is called
 24615          from move_it_in_display_line_to: HPOS is incremented only
 24616          when we are in the visible portion of the glyph row.  */
 24617       if (it->current_x > it->first_visible_x)
 24618         it->hpos++;
 24619       if (p)
 24620         {
 24621           *p++ = *g;
 24622           (*u)++;
 24623         }
 24624     }
 24625 
 24626   /* Update IT's metrics due to glyphs produced for line numbers.
 24627      Don't do that for rows beyond ZV, to avoid displaying a cursor of
 24628      different dimensions there.  */
 24629   if (!beyond_zv)
 24630     {
 24631       if (it->glyph_row)
 24632         {
 24633           struct glyph_row *row = it->glyph_row;
 24634 
 24635           it->max_ascent = max (row->ascent, tem_it.max_ascent);
 24636           it->max_descent = max (row->height - row->ascent, tem_it.max_descent);
 24637           it->max_phys_ascent = max (row->phys_ascent, tem_it.max_phys_ascent);
 24638           it->max_phys_descent = max (row->phys_height - row->phys_ascent,
 24639                                       tem_it.max_phys_descent);
 24640         }
 24641       else
 24642         {
 24643           it->max_ascent = max (it->max_ascent, tem_it.max_ascent);
 24644           it->max_descent = max (it->max_descent, tem_it.max_descent);
 24645           it->max_phys_ascent = max (it->max_phys_ascent,
 24646                                      tem_it.max_phys_ascent);
 24647           it->max_phys_descent = max (it->max_phys_descent,
 24648                                       tem_it.max_phys_descent);
 24649         }
 24650     }
 24651 
 24652   it->line_number_produced_p = true;
 24653 
 24654   bidi_unshelve_cache (itdata, false);
 24655 }
 24656 
 24657 /* Return true if this glyph row needs a line number to be produced
 24658    for it.  */
 24659 static bool
 24660 should_produce_line_number (struct it *it)
 24661 {
 24662   if (NILP (Vdisplay_line_numbers))
 24663     return false;
 24664 
 24665   /* Don't display line numbers in minibuffer windows.  */
 24666   if (MINI_WINDOW_P (it->w))
 24667     return false;
 24668 
 24669 #ifdef HAVE_WINDOW_SYSTEM
 24670   /* Don't display line number in tooltip frames.  */
 24671   if (FRAME_TOOLTIP_P (XFRAME (WINDOW_FRAME (it->w))))
 24672     return false;
 24673 #endif
 24674 
 24675   /* If the character at current position has a non-nil special
 24676      property, disable line numbers for this row.  This is for
 24677      packages such as company-mode, which need this for their tricky
 24678      layout, where line numbers get in the way.  */
 24679   Lisp_Object val = Fget_char_property (make_fixnum (IT_CHARPOS (*it)),
 24680                                         Qdisplay_line_numbers_disable,
 24681                                         it->window);
 24682   /* For ZV, we need to also look in empty overlays at that point,
 24683      because get-char-property always returns nil for ZV, except if
 24684      the property is in 'default-text-properties'.  */
 24685   if (NILP (val) && IT_CHARPOS (*it) >= ZV)
 24686     val = disable_line_numbers_overlay_at_eob ();
 24687   return NILP (val) ? true : false;
 24688 }
 24689 
 24690 /* Return true if ROW has no glyphs except those inserted by the
 24691    display engine.  This is needed for indicate-empty-lines and
 24692    similar features when the glyph row starts with glyphs which didn't
 24693    come from buffer or string.  */
 24694 static bool
 24695 row_text_area_empty (struct glyph_row *row)
 24696 {
 24697   if (!row->reversed_p)
 24698     {
 24699       for (struct glyph *g = row->glyphs[TEXT_AREA];
 24700            g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 24701            g++)
 24702         if (!NILP (g->object) || g->charpos > 0)
 24703           return false;
 24704     }
 24705   else
 24706     {
 24707       for (struct glyph *g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 24708            g > row->glyphs[TEXT_AREA];
 24709            g--)
 24710         if (!NILP ((g - 1)->object) || (g - 1)->charpos > 0)
 24711           return false;
 24712     }
 24713 
 24714   return true;
 24715 }
 24716 
 24717 /* Construct the glyph row IT->glyph_row in the desired matrix of
 24718    IT->w from text at the current position of IT.  See dispextern.h
 24719    for an overview of struct it.  Value is true if
 24720    IT->glyph_row displays text, as opposed to a line displaying ZV
 24721    only.  CURSOR_VPOS is the window-relative vertical position of
 24722    the glyph row displaying the cursor, or -1 if unknown.  */
 24723 
 24724 static bool
 24725 display_line (struct it *it, int cursor_vpos)
 24726 {
 24727   struct glyph_row *row = it->glyph_row;
 24728   Lisp_Object overlay_arrow_string;
 24729   struct it wrap_it;
 24730   void *wrap_data = NULL;
 24731   bool may_wrap = false;
 24732   int wrap_x UNINIT;
 24733   int wrap_row_used = -1;
 24734   int wrap_row_ascent UNINIT, wrap_row_height UNINIT;
 24735   int wrap_row_phys_ascent UNINIT, wrap_row_phys_height UNINIT;
 24736   int wrap_row_extra_line_spacing UNINIT;
 24737   ptrdiff_t wrap_row_min_pos UNINIT, wrap_row_min_bpos UNINIT;
 24738   ptrdiff_t wrap_row_max_pos UNINIT, wrap_row_max_bpos UNINIT;
 24739   int cvpos;
 24740   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
 24741   ptrdiff_t min_bpos UNINIT, max_bpos UNINIT;
 24742   bool pending_handle_line_prefix = false;
 24743   int tab_line = window_wants_tab_line (it->w);
 24744   int header_line = window_wants_header_line (it->w);
 24745   bool hscroll_this_line = (cursor_vpos >= 0
 24746                             && it->vpos == cursor_vpos - tab_line - header_line
 24747                             && hscrolling_current_line_p (it->w));
 24748   int first_visible_x = it->first_visible_x;
 24749   int last_visible_x = it->last_visible_x;
 24750   int x_incr = 0;
 24751 
 24752   /* We always start displaying at hpos zero even if hscrolled.  */
 24753   eassert (it->hpos == 0 && it->current_x == 0);
 24754 
 24755   if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
 24756       >= it->w->desired_matrix->nrows)
 24757     {
 24758       it->w->nrows_scale_factor++;
 24759       it->f->fonts_changed = true;
 24760       return false;
 24761     }
 24762 
 24763   /* Clear the result glyph row and enable it.  */
 24764   prepare_desired_row (it->w, row, false);
 24765 
 24766   row->y = it->current_y;
 24767   row->start = it->start;
 24768   row->continuation_lines_width = it->continuation_lines_width;
 24769   row->displays_text_p = true;
 24770   row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
 24771   it->starts_in_middle_of_char_p = false;
 24772   it->stretch_adjust = 0;
 24773   it->line_number_produced_p = false;
 24774 
 24775   /* If we are going to display the cursor's line, account for the
 24776      hscroll of that line.  We subtract the window's min_hscroll,
 24777      because that was already accounted for in init_iterator.  */
 24778   if (hscroll_this_line)
 24779     x_incr =
 24780       (window_hscroll_limited (it->w, it->f) - it->w->min_hscroll)
 24781       * FRAME_COLUMN_WIDTH (it->f);
 24782 
 24783   bool line_number_needed = should_produce_line_number (it);
 24784 
 24785   /* Move over display elements that are not visible because we are
 24786      hscrolled.  This may stop at an x-position < first_visible_x
 24787      if the first glyph is partially visible or if we hit a line end.  */
 24788   if (it->current_x < it->first_visible_x + x_incr)
 24789     {
 24790       enum move_it_result move_result;
 24791 
 24792       this_line_min_pos = row->start.pos;
 24793       if (hscroll_this_line)
 24794         {
 24795           it->first_visible_x += x_incr;
 24796           it->last_visible_x  += x_incr;
 24797         }
 24798       if (current_buffer->long_line_optimizations_p
 24799           && it->line_wrap == TRUNCATE
 24800           && window_hscroll_limited (it->w, it->f) > large_hscroll_threshold)
 24801         {
 24802           /* Special optimization for very long and truncated lines
 24803              which are hscrolled far to the left: jump directly to the
 24804              (approximate) position that is visible, instead of slowly
 24805              walking there.  */
 24806           ptrdiff_t chars_to_skip =
 24807             it->first_visible_x / FRAME_COLUMN_WIDTH (it->f);
 24808           move_result = fast_move_it_horizontally (it, chars_to_skip);
 24809 
 24810           if (move_result == MOVE_X_REACHED)
 24811             it->current_x = it->first_visible_x;
 24812           else  /* use arbitrary value < first_visible_x */
 24813             it->current_x = it->first_visible_x - FRAME_COLUMN_WIDTH (it->f);
 24814         }
 24815       else
 24816         move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
 24817                                                   MOVE_TO_POS | MOVE_TO_X);
 24818       /* If we are under a large hscroll, move_it_in_display_line_to
 24819          could hit the end of the line without reaching
 24820          first_visible_x.  Pretend that we did reach it.  This is
 24821          especially important on a TTY, where we will call
 24822          extend_face_to_end_of_line, which needs to know how many
 24823          blank glyphs to produce.  */
 24824       if (it->current_x < it->first_visible_x
 24825           && (move_result == MOVE_NEWLINE_OR_CR
 24826               || move_result == MOVE_POS_MATCH_OR_ZV))
 24827         it->current_x = it->first_visible_x;
 24828 
 24829       /* In case move_it_in_display_line_to above "produced" the line
 24830          number.  */
 24831       it->line_number_produced_p = false;
 24832 
 24833       /* Record the smallest positions seen while we moved over
 24834          display elements that are not visible.  This is needed by
 24835          redisplay_internal for optimizing the case where the cursor
 24836          stays inside the same line.  The rest of this function only
 24837          considers positions that are actually displayed, so
 24838          RECORD_MAX_MIN_POS will not otherwise record positions that
 24839          are hscrolled to the left of the left edge of the window.  */
 24840       min_pos = CHARPOS (this_line_min_pos);
 24841       min_bpos = BYTEPOS (this_line_min_pos);
 24842 
 24843       /* Produce line number, if needed.  */
 24844       if (line_number_needed)
 24845         maybe_produce_line_number (it);
 24846     }
 24847   else if (it->area == TEXT_AREA)
 24848     {
 24849       /* Line numbers should precede the line-prefix or wrap-prefix.  */
 24850       if (line_number_needed)
 24851         maybe_produce_line_number (it);
 24852 
 24853       /* We only do this when not calling move_it_in_display_line_to
 24854          above, because that function calls itself handle_line_prefix.  */
 24855       handle_line_prefix (it);
 24856     }
 24857   else
 24858     {
 24859       /* Line-prefix and wrap-prefix are always displayed in the text
 24860          area.  But if this is the first call to display_line after
 24861          init_iterator, the iterator might have been set up to write
 24862          into a marginal area, e.g. if the line begins with some
 24863          display property that writes to the margins.  So we need to
 24864          wait with the call to handle_line_prefix until whatever
 24865          writes to the margin has done its job.  */
 24866       pending_handle_line_prefix = true;
 24867     }
 24868 
 24869   /* Get the initial row height.  This is either the height of the
 24870      text hscrolled, if there is any, or zero.  */
 24871   row->ascent = it->max_ascent;
 24872   row->height = it->max_ascent + it->max_descent;
 24873   row->phys_ascent = it->max_phys_ascent;
 24874   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 24875   row->extra_line_spacing = it->max_extra_line_spacing;
 24876 
 24877 /* Utility macro to record max and min buffer positions seen until now.  */
 24878 #define RECORD_MAX_MIN_POS(IT)                                  \
 24879   do                                                            \
 24880     {                                                           \
 24881       bool composition_p                                        \
 24882         = !STRINGP ((IT)->string) && ((IT)->what == IT_COMPOSITION); \
 24883       ptrdiff_t current_pos =                                   \
 24884         composition_p ? (IT)->cmp_it.charpos                    \
 24885                       : IT_CHARPOS (*(IT));                     \
 24886       ptrdiff_t current_bpos =                                  \
 24887         composition_p ? CHAR_TO_BYTE (current_pos)              \
 24888                       : IT_BYTEPOS (*(IT));                     \
 24889       if (current_pos < min_pos)                                \
 24890         {                                                       \
 24891           min_pos = current_pos;                                \
 24892           min_bpos = current_bpos;                              \
 24893         }                                                       \
 24894       if (IT_CHARPOS (*it) > max_pos)                           \
 24895         {                                                       \
 24896           max_pos = IT_CHARPOS (*it);                           \
 24897           max_bpos = IT_BYTEPOS (*it);                          \
 24898         }                                                       \
 24899     }                                                           \
 24900   while (false)
 24901 
 24902   /* Loop generating characters.  The loop is left with IT on the next
 24903      character to display.  */
 24904   while (true)
 24905     {
 24906       int n_glyphs_before, hpos_before, x_before;
 24907       int x, nglyphs;
 24908       int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
 24909 
 24910       /* Retrieve the next thing to display.  Value is false if end of
 24911          buffer reached.  */
 24912       if (!get_next_display_element (it))
 24913         {
 24914           bool row_has_glyphs = false;
 24915           /* Maybe add a space at the end of this line that is used to
 24916              display the cursor there under X.  Set the charpos of the
 24917              first glyph of blank lines not corresponding to any text
 24918              to -1.  */
 24919           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 24920             row->exact_window_width_line_p = true;
 24921           else if ((append_space_for_newline (it, true)
 24922                     && row->used[TEXT_AREA] == 1)
 24923                    || row->used[TEXT_AREA] == 0
 24924                    || (row_has_glyphs = row_text_area_empty (row)))
 24925             {
 24926               row->glyphs[TEXT_AREA]->charpos = -1;
 24927               /* Don't reset the displays_text_p flag if we are
 24928                  displaying line numbers or line-prefix.  */
 24929               if (!row_has_glyphs)
 24930                 row->displays_text_p = false;
 24931 
 24932               if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
 24933                   && (!MINI_WINDOW_P (it->w)))
 24934                 row->indicate_empty_line_p = true;
 24935             }
 24936 
 24937           it->continuation_lines_width = 0;
 24938           /* Reset those iterator values set from display property
 24939              values.  This is for the case when the display property
 24940              ends at ZV, and is not a replacing property, so pop_it is
 24941              not called.  */
 24942           it->font_height = Qnil;
 24943           it->voffset = 0;
 24944           row->ends_at_zv_p = true;
 24945           /* A row that displays right-to-left text must always have
 24946              its last face extended all the way to the end of line,
 24947              even if this row ends in ZV, because we still write to
 24948              the screen left to right.  We also need to extend the
 24949              last face if the default face is remapped to some
 24950              different face, otherwise the functions that clear
 24951              portions of the screen will clear with the default face's
 24952              background color.  */
 24953           if (row->reversed_p
 24954               || lookup_basic_face (it->w, it->f, DEFAULT_FACE_ID)
 24955               != DEFAULT_FACE_ID)
 24956             extend_face_to_end_of_line (it);
 24957           break;
 24958         }
 24959 
 24960       /* Now, get the metrics of what we want to display.  This also
 24961          generates glyphs in `row' (which is IT->glyph_row).  */
 24962       n_glyphs_before = row->used[TEXT_AREA];
 24963       x = it->current_x;
 24964 
 24965       /* Remember the line height so far in case the next element doesn't
 24966          fit on the line.  */
 24967       if (it->line_wrap != TRUNCATE)
 24968         {
 24969           ascent = it->max_ascent;
 24970           descent = it->max_descent;
 24971           phys_ascent = it->max_phys_ascent;
 24972           phys_descent = it->max_phys_descent;
 24973 
 24974           if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
 24975             {
 24976               bool next_may_wrap = may_wrap;
 24977               /* Can we wrap after this character?  */
 24978               if (char_can_wrap_after (it))
 24979                 next_may_wrap = true;
 24980               else
 24981                 next_may_wrap = false;
 24982               /* Can we wrap here? */
 24983               if (may_wrap && char_can_wrap_before (it))
 24984                 {
 24985                   SAVE_IT (wrap_it, *it, wrap_data);
 24986                   wrap_x = x;
 24987                   wrap_row_used = row->used[TEXT_AREA];
 24988                   wrap_row_ascent = row->ascent;
 24989                   wrap_row_height = row->height;
 24990                   wrap_row_phys_ascent = row->phys_ascent;
 24991                   wrap_row_phys_height = row->phys_height;
 24992                   wrap_row_extra_line_spacing = row->extra_line_spacing;
 24993                   wrap_row_min_pos = min_pos;
 24994                   wrap_row_min_bpos = min_bpos;
 24995                   wrap_row_max_pos = max_pos;
 24996                   wrap_row_max_bpos = max_bpos;
 24997                 }
 24998               /* Update may_wrap for the next iteration.  */
 24999               may_wrap = next_may_wrap;
 25000             }
 25001         }
 25002 
 25003       PRODUCE_GLYPHS (it);
 25004 
 25005       /* If this display element was in marginal areas, continue with
 25006          the next one.  */
 25007       if (it->area != TEXT_AREA)
 25008         {
 25009           row->ascent = max (row->ascent, it->max_ascent);
 25010           row->height = max (row->height, it->max_ascent + it->max_descent);
 25011           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25012           row->phys_height = max (row->phys_height,
 25013                                   it->max_phys_ascent + it->max_phys_descent);
 25014           row->extra_line_spacing = max (row->extra_line_spacing,
 25015                                          it->max_extra_line_spacing);
 25016           set_iterator_to_next (it, true);
 25017           /* If we didn't handle the line/wrap prefix above, and the
 25018              call to set_iterator_to_next just switched to TEXT_AREA,
 25019              process the prefix now.  */
 25020           if (it->area == TEXT_AREA && pending_handle_line_prefix)
 25021             {
 25022               /* Line numbers should precede the line-prefix or wrap-prefix.  */
 25023               if (line_number_needed)
 25024                 maybe_produce_line_number (it);
 25025 
 25026               pending_handle_line_prefix = false;
 25027               handle_line_prefix (it);
 25028             }
 25029           continue;
 25030         }
 25031 
 25032       /* Does the display element fit on the line?  If we truncate
 25033          lines, we should draw past the right edge of the window.  If
 25034          we don't truncate, we want to stop so that we can display the
 25035          continuation glyph before the right margin.  If lines are
 25036          continued, there are two possible strategies for characters
 25037          resulting in more than 1 glyph (e.g. tabs): Display as many
 25038          glyphs as possible in this line and leave the rest for the
 25039          continuation line, or display the whole element in the next
 25040          line.  Original redisplay did the former, so we do it also.  */
 25041       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 25042       hpos_before = it->hpos;
 25043       x_before = x;
 25044 
 25045       if (/* Not a newline.  */
 25046           nglyphs > 0
 25047           /* Glyphs produced fit entirely in the line.  */
 25048           && it->current_x < it->last_visible_x)
 25049         {
 25050           it->hpos += nglyphs;
 25051           row->ascent = max (row->ascent, it->max_ascent);
 25052           row->height = max (row->height, it->max_ascent + it->max_descent);
 25053           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25054           row->phys_height = max (row->phys_height,
 25055                                   it->max_phys_ascent + it->max_phys_descent);
 25056           row->extra_line_spacing = max (row->extra_line_spacing,
 25057                                          it->max_extra_line_spacing);
 25058           if (it->current_x - it->pixel_width < it->first_visible_x
 25059               /* When line numbers are displayed, row->x should not be
 25060                  offset, as the first glyph after the line number can
 25061                  never be partially visible.  */
 25062               && !line_number_needed
 25063               /* In R2L rows, we arrange in extend_face_to_end_of_line
 25064                  to add a right offset to the line, by a suitable
 25065                  change to the stretch glyph that is the leftmost
 25066                  glyph of the line.  */
 25067               && !row->reversed_p)
 25068             row->x = x - it->first_visible_x;
 25069           /* Record the maximum and minimum buffer positions seen so
 25070              far in glyphs that will be displayed by this row.  */
 25071           if (it->bidi_p)
 25072             RECORD_MAX_MIN_POS (it);
 25073         }
 25074       else
 25075         {
 25076           int i, new_x;
 25077           struct glyph *glyph;
 25078 
 25079           for (i = 0; i < nglyphs; ++i, x = new_x)
 25080             {
 25081               /* Identify the glyphs added by the last call to
 25082                  PRODUCE_GLYPHS.  In R2L rows, they are prepended to
 25083                  the previous glyphs.  */
 25084               if (!row->reversed_p)
 25085                 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 25086               else
 25087                 glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
 25088               new_x = x + glyph->pixel_width;
 25089 
 25090               if (/* Lines are continued.  */
 25091                   it->line_wrap != TRUNCATE
 25092                   && (/* Glyph doesn't fit on the line.  */
 25093                       new_x > it->last_visible_x
 25094                       /* Or it fits exactly on a window system frame.  */
 25095                       || (new_x == it->last_visible_x
 25096                           && FRAME_WINDOW_P (it->f)
 25097                           && (row->reversed_p
 25098                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25099                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
 25100                 {
 25101                   /* End of a continued line.  */
 25102 
 25103                   if (it->hpos == 0
 25104                       || (new_x == it->last_visible_x
 25105                           && FRAME_WINDOW_P (it->f)
 25106                           && (row->reversed_p
 25107                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25108                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
 25109                     {
 25110                       /* Current glyph is the only one on the line or
 25111                          fits exactly on the line.  We must continue
 25112                          the line because we can't draw the cursor
 25113                          after the glyph.  */
 25114                       row->continued_p = true;
 25115                       it->current_x = new_x;
 25116                       it->continuation_lines_width += new_x;
 25117                       ++it->hpos;
 25118                       if (i == nglyphs - 1)
 25119                         {
 25120                           /* If line-wrap is on, check if a previous
 25121                              wrap point was found.  */
 25122                           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
 25123                               && wrap_row_used > 0 /* Found.  */
 25124                               /* Even if there is a previous wrap
 25125                                  point, continue the line here as
 25126                                  usual, if (i) the previous character
 25127                                  allows wrapping after it, AND (ii)
 25128                                  the current character allows wrapping
 25129                                  before it.  Because this is a valid
 25130                                  break point, we can just continue to
 25131                                  the next line at here, there is no
 25132                                  need to wrap early at the previous
 25133                                  wrap point.  */
 25134                               && (!may_wrap || !char_can_wrap_before (it)))
 25135                             goto back_to_wrap;
 25136 
 25137                           /* Record the maximum and minimum buffer
 25138                              positions seen so far in glyphs that will be
 25139                              displayed by this row.  */
 25140                           if (it->bidi_p)
 25141                             RECORD_MAX_MIN_POS (it);
 25142                           set_iterator_to_next (it, true);
 25143                           if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25144                             {
 25145                               if (!get_next_display_element (it))
 25146                                 {
 25147                                   row->exact_window_width_line_p = true;
 25148                                   it->continuation_lines_width = 0;
 25149                                   it->font_height = Qnil;
 25150                                   it->voffset = 0;
 25151                                   row->continued_p = false;
 25152                                   row->ends_at_zv_p = true;
 25153                                 }
 25154                               else if (ITERATOR_AT_END_OF_LINE_P (it))
 25155                                 {
 25156                                   row->continued_p = false;
 25157                                   row->exact_window_width_line_p = true;
 25158                                 }
 25159                               /* If line-wrap is on, check if a
 25160                                  previous wrap point was found.  */
 25161                               else if (wrap_row_used > 0
 25162                                        /* Even if there is a previous
 25163                                           wrap point, continue the
 25164                                           line here as usual, if (i)
 25165                                           the previous character was a
 25166                                           space or tab AND (ii) the
 25167                                           current character is not,
 25168                                           AND (iii) the current
 25169                                           character allows wrapping
 25170                                           before it.  */
 25171                                        && (!may_wrap || !char_can_wrap_before (it)))
 25172                                 goto back_to_wrap;
 25173 
 25174                             }
 25175                         }
 25176                       else if (it->bidi_p)
 25177                         RECORD_MAX_MIN_POS (it);
 25178                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25179                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25180                         extend_face_to_end_of_line (it);
 25181                     }
 25182                   else if (CHAR_GLYPH_PADDING_P (*glyph)
 25183                            && !FRAME_WINDOW_P (it->f))
 25184                     {
 25185                       /* A padding glyph that doesn't fit on this line.
 25186                          This means the whole character doesn't fit
 25187                          on the line.  */
 25188                       if (row->reversed_p)
 25189                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25190                                                - n_glyphs_before);
 25191                       row->used[TEXT_AREA] = n_glyphs_before;
 25192 
 25193                       /* Fill the rest of the row with continuation
 25194                          glyphs like in 20.x.  */
 25195                       while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
 25196                              < row->glyphs[1 + TEXT_AREA])
 25197                         produce_special_glyphs (it, IT_CONTINUATION);
 25198 
 25199                       row->continued_p = true;
 25200                       it->current_x = x_before;
 25201                       it->continuation_lines_width += x_before;
 25202 
 25203                       /* Restore the height to what it was before the
 25204                          element not fitting on the line.  */
 25205                       it->max_ascent = ascent;
 25206                       it->max_descent = descent;
 25207                       it->max_phys_ascent = phys_ascent;
 25208                       it->max_phys_descent = phys_descent;
 25209                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25210                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25211                         extend_face_to_end_of_line (it);
 25212                     }
 25213                   else if (wrap_row_used > 0)
 25214                     {
 25215                     back_to_wrap:
 25216                       if (row->reversed_p)
 25217                         unproduce_glyphs (it,
 25218                                           row->used[TEXT_AREA] - wrap_row_used);
 25219                       RESTORE_IT (it, &wrap_it, wrap_data);
 25220                       it->continuation_lines_width += wrap_x;
 25221                       row->used[TEXT_AREA] = wrap_row_used;
 25222                       row->ascent = wrap_row_ascent;
 25223                       row->height = wrap_row_height;
 25224                       row->phys_ascent = wrap_row_phys_ascent;
 25225                       row->phys_height = wrap_row_phys_height;
 25226                       row->extra_line_spacing = wrap_row_extra_line_spacing;
 25227                       min_pos = wrap_row_min_pos;
 25228                       min_bpos = wrap_row_min_bpos;
 25229                       max_pos = wrap_row_max_pos;
 25230                       max_bpos = wrap_row_max_bpos;
 25231                       row->continued_p = true;
 25232                       row->ends_at_zv_p = false;
 25233                       row->exact_window_width_line_p = false;
 25234 
 25235                       /* Make sure that a non-default face is extended
 25236                          up to the right margin of the window.  */
 25237                       extend_face_to_end_of_line (it);
 25238                     }
 25239                   else if ((it->what == IT_CHARACTER
 25240                             || it->what == IT_STRETCH
 25241                             || it->what == IT_COMPOSITION)
 25242                            && it->c == '\t' && FRAME_WINDOW_P (it->f))
 25243                     {
 25244                       /* A TAB that extends past the right edge of the
 25245                          window.  This produces a single glyph on
 25246                          window system frames.  We leave the glyph in
 25247                          this row and let it fill the row, but don't
 25248                          consume the TAB.  */
 25249                       if ((row->reversed_p
 25250                            ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25251                            : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25252                         produce_special_glyphs (it, IT_CONTINUATION);
 25253                       it->continuation_lines_width += it->last_visible_x;
 25254                       row->ends_in_middle_of_char_p = true;
 25255                       row->continued_p = true;
 25256                       glyph->pixel_width = it->last_visible_x - x;
 25257                       it->starts_in_middle_of_char_p = true;
 25258                       if (WINDOW_LEFT_MARGIN_WIDTH (it->w) > 0
 25259                           || WINDOW_RIGHT_MARGIN_WIDTH (it->w) > 0)
 25260                         extend_face_to_end_of_line (it);
 25261                     }
 25262                   else
 25263                     {
 25264                       /* Something other than a TAB that draws past
 25265                          the right edge of the window.  Restore
 25266                          positions to values before the element.  */
 25267                       if (row->reversed_p)
 25268                         unproduce_glyphs (it, row->used[TEXT_AREA]
 25269                                                - (n_glyphs_before + i));
 25270                       row->used[TEXT_AREA] = n_glyphs_before + i;
 25271 
 25272                       /* Display continuation glyphs.  */
 25273                       it->current_x = x_before;
 25274                       it->continuation_lines_width += x;
 25275                       if (!FRAME_WINDOW_P (it->f)
 25276                           || (row->reversed_p
 25277                               ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25278                               : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25279                         produce_special_glyphs (it, IT_CONTINUATION);
 25280                       row->continued_p = true;
 25281 
 25282                       extend_face_to_end_of_line (it);
 25283 
 25284                       if (nglyphs > 1 && i > 0)
 25285                         {
 25286                           row->ends_in_middle_of_char_p = true;
 25287                           it->starts_in_middle_of_char_p = true;
 25288                         }
 25289 
 25290                       /* Restore the height to what it was before the
 25291                          element not fitting on the line.  */
 25292                       it->max_ascent = ascent;
 25293                       it->max_descent = descent;
 25294                       it->max_phys_ascent = phys_ascent;
 25295                       it->max_phys_descent = phys_descent;
 25296                     }
 25297 
 25298                   break;
 25299                 }
 25300               else if (new_x > it->first_visible_x)
 25301                 {
 25302                   /* Increment number of glyphs actually displayed.  */
 25303                   ++it->hpos;
 25304 
 25305                   /* Record the maximum and minimum buffer positions
 25306                      seen so far in glyphs that will be displayed by
 25307                      this row.  */
 25308                   if (it->bidi_p)
 25309                     RECORD_MAX_MIN_POS (it);
 25310 
 25311                   if (x < it->first_visible_x && !row->reversed_p
 25312                       && !line_number_needed)
 25313                     /* Glyph is partially visible, i.e. row starts at
 25314                        negative X position.  Don't do that in R2L
 25315                        rows, where we arrange to add a right offset to
 25316                        the line in extend_face_to_end_of_line, by a
 25317                        suitable change to the stretch glyph that is
 25318                        the leftmost glyph of the line.  */
 25319                     row->x = x - it->first_visible_x;
 25320                   /* When the last glyph of an R2L row only fits
 25321                      partially on the line, we need to set row->x to a
 25322                      negative offset, so that the leftmost glyph is
 25323                      the one that is partially visible.  But if we are
 25324                      going to produce the truncation glyph, this will
 25325                      be taken care of in produce_special_glyphs.  */
 25326                   if (row->reversed_p
 25327                       && new_x > it->last_visible_x
 25328                       && !line_number_needed
 25329                       && !(it->line_wrap == TRUNCATE
 25330                            && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
 25331                     {
 25332                       eassert (FRAME_WINDOW_P (it->f));
 25333                       row->x = it->last_visible_x - new_x;
 25334                     }
 25335                 }
 25336               else
 25337                 {
 25338                   /* Glyph is completely off the left margin of the
 25339                      window.  This should not happen because of the
 25340                      move_it_in_display_line at the start of this
 25341                      function, unless the text display area of the
 25342                      window is empty.  */
 25343                   eassert (it->first_visible_x <= it->last_visible_x);
 25344                 }
 25345             }
 25346           /* Even if this display element produced no glyphs at all,
 25347              we want to record its position.  */
 25348           if (it->bidi_p && nglyphs == 0)
 25349             RECORD_MAX_MIN_POS (it);
 25350 
 25351           row->ascent = max (row->ascent, it->max_ascent);
 25352           row->height = max (row->height, it->max_ascent + it->max_descent);
 25353           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 25354           row->phys_height = max (row->phys_height,
 25355                                   it->max_phys_ascent + it->max_phys_descent);
 25356           row->extra_line_spacing = max (row->extra_line_spacing,
 25357                                          it->max_extra_line_spacing);
 25358 
 25359           /* End of this display line if row is continued.  */
 25360           if (row->continued_p || row->ends_at_zv_p)
 25361             break;
 25362         }
 25363 
 25364     at_end_of_line:
 25365       /* Is this a line end?  If yes, we're also done, after making
 25366          sure that a non-default face is extended up to the right
 25367          margin of the window.  */
 25368       if (ITERATOR_AT_END_OF_LINE_P (it))
 25369         {
 25370           int used_before = row->used[TEXT_AREA];
 25371 
 25372           row->ends_in_newline_from_string_p = STRINGP (it->object);
 25373 
 25374           /* Add a space at the end of the line that is used to
 25375              display the cursor there.  */
 25376           if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25377             append_space_for_newline (it, false);
 25378 
 25379           /* Extend the face to the end of the line.  */
 25380           extend_face_to_end_of_line (it);
 25381 
 25382           /* Make sure we have the position.  */
 25383           if (used_before == 0)
 25384             row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
 25385 
 25386           /* Record the position of the newline, for use in
 25387              find_row_edges.  */
 25388           it->eol_pos = it->current.pos;
 25389 
 25390           /* Consume the line end.  This skips over invisible lines.  */
 25391           set_iterator_to_next (it, true);
 25392           it->continuation_lines_width = 0;
 25393           break;
 25394         }
 25395 
 25396       /* Detect overly-wide wrap-prefixes made of (space ...) display
 25397          properties.  When such a wrap prefix reaches past the right
 25398          margin of the window, we need to avoid the call to
 25399          set_iterator_to_next below, so that it->line_wrap is left at
 25400          its TRUNCATE value wisely set by handle_line_prefix.
 25401          Otherwise, set_iterator_to_next will pop the iterator stack,
 25402          restore it->line_wrap, and redisplay might infloop.  */
 25403       bool overwide_wrap_prefix =
 25404         CONSP (it->object) && EQ (XCAR (it->object), Qspace)
 25405         && it->sp > 0 && it->method == GET_FROM_STRETCH
 25406         && it->current_x >= it->last_visible_x
 25407         && it->continuation_lines_width > 0
 25408         && it->line_wrap == TRUNCATE && it->stack[0].line_wrap != TRUNCATE;
 25409 
 25410       /* Proceed with next display element.  Note that this skips
 25411          over lines invisible because of selective display.  */
 25412       if (!overwide_wrap_prefix)
 25413         set_iterator_to_next (it, true);
 25414 
 25415       /* If we truncate lines, we are done when the last displayed
 25416          glyphs reach past the right margin of the window.  */
 25417       if (it->line_wrap == TRUNCATE
 25418           && ((FRAME_WINDOW_P (it->f)
 25419                /* Images are preprocessed in produce_image_glyph such
 25420                   that they are cropped at the right edge of the
 25421                   window, so an image glyph will always end exactly at
 25422                   last_visible_x, even if there's no right fringe.  */
 25423                && ((row->reversed_p
 25424                     ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25425                     : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
 25426                    || it->what == IT_IMAGE))
 25427               ? (it->current_x >= it->last_visible_x)
 25428               : (it->current_x > it->last_visible_x)))
 25429         {
 25430           /* Maybe add truncation glyphs.  */
 25431           if (!FRAME_WINDOW_P (it->f)
 25432               || (row->reversed_p
 25433                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
 25434                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
 25435             {
 25436               int i, n;
 25437 
 25438               if (!row->reversed_p)
 25439                 {
 25440                   for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
 25441                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25442                       break;
 25443                 }
 25444               else
 25445                 {
 25446                   for (i = 0; i < row->used[TEXT_AREA]; i++)
 25447                     if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
 25448                       break;
 25449                   /* Remove any padding glyphs at the front of ROW, to
 25450                      make room for the truncation glyphs we will be
 25451                      adding below.  The loop below always inserts at
 25452                      least one truncation glyph, so also remove the
 25453                      last glyph added to ROW.  */
 25454                   unproduce_glyphs (it, i + 1);
 25455                   /* Adjust i for the loop below.  */
 25456                   i = row->used[TEXT_AREA] - (i + 1);
 25457                 }
 25458 
 25459               /* produce_special_glyphs overwrites the last glyph, so
 25460                  we don't want that if we want to keep that last
 25461                  glyph, which means it's an image.  */
 25462               if (it->current_x > it->last_visible_x)
 25463                 {
 25464                   it->current_x = x_before;
 25465                   if (!FRAME_WINDOW_P (it->f))
 25466                     {
 25467                       for (n = row->used[TEXT_AREA]; i < n; ++i)
 25468                         {
 25469                           row->used[TEXT_AREA] = i;
 25470                           produce_special_glyphs (it, IT_TRUNCATION);
 25471                         }
 25472                     }
 25473                   else
 25474                     {
 25475                       row->used[TEXT_AREA] = i;
 25476                       produce_special_glyphs (it, IT_TRUNCATION);
 25477                     }
 25478                   it->hpos = hpos_before;
 25479                 }
 25480             }
 25481           else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
 25482             {
 25483               /* Don't truncate if we can overflow newline into fringe.  */
 25484               if (!get_next_display_element (it))
 25485                 {
 25486                   it->continuation_lines_width = 0;
 25487                   it->font_height = Qnil;
 25488                   it->voffset = 0;
 25489                   row->ends_at_zv_p = true;
 25490                   row->exact_window_width_line_p = true;
 25491                   break;
 25492                 }
 25493               if (ITERATOR_AT_END_OF_LINE_P (it))
 25494                 {
 25495                   row->exact_window_width_line_p = true;
 25496                   goto at_end_of_line;
 25497                 }
 25498               it->current_x = x_before;
 25499               it->hpos = hpos_before;
 25500             }
 25501 
 25502           row->truncated_on_right_p = true;
 25503           it->continuation_lines_width = 0;
 25504           reseat_at_next_visible_line_start (it, false);
 25505           /* We insist below that IT's position be at ZV because in
 25506              bidi-reordered lines the character at visible line start
 25507              might not be the character that follows the newline in
 25508              the logical order.  */
 25509           if (IT_BYTEPOS (*it) > BEG_BYTE)
 25510             row->ends_at_zv_p =
 25511               IT_BYTEPOS (*it) >= ZV_BYTE
 25512               && (ZV_BYTE <= 1 || FETCH_BYTE (ZV_BYTE - 1) != '\n');
 25513           else
 25514             row->ends_at_zv_p = false;
 25515           break;
 25516         }
 25517     }
 25518 
 25519   if (wrap_data)
 25520     bidi_unshelve_cache (wrap_data, true);
 25521 
 25522   /* If line is not empty and hscrolled, maybe insert truncation glyphs
 25523      at the left window margin.  */
 25524   if (it->first_visible_x
 25525       && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
 25526     {
 25527       if (!FRAME_WINDOW_P (it->f)
 25528           || (((row->reversed_p
 25529                 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 25530                 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 25531               /* Don't let insert_left_trunc_glyphs overwrite the
 25532                  first glyph of the row if it is an image.  */
 25533               && row->glyphs[TEXT_AREA]->type != IMAGE_GLYPH))
 25534         insert_left_trunc_glyphs (it);
 25535       row->truncated_on_left_p = true;
 25536     }
 25537 
 25538   /* Remember the position at which this line ends.
 25539 
 25540      BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
 25541      cannot be before the call to find_row_edges below, since that is
 25542      where these positions are determined. */
 25543   row->end = it->current;
 25544   if (!it->bidi_p)
 25545     {
 25546       row->minpos = row->start.pos;
 25547       row->maxpos = row->end.pos;
 25548     }
 25549   else
 25550     {
 25551       /* ROW->minpos and ROW->maxpos must be the smallest and
 25552          `1 + the largest' buffer positions in ROW.  But if ROW was
 25553          bidi-reordered, these two positions can be anywhere in the
 25554          row, so we must determine them now.  */
 25555       find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
 25556     }
 25557 
 25558   /* If the start of this line is the overlay arrow-position, then
 25559      mark this glyph row as the one containing the overlay arrow.
 25560      This is clearly a mess with variable size fonts.  It would be
 25561      better to let it be displayed like cursors under X.  */
 25562   if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
 25563       && (overlay_arrow_string = overlay_arrow_at_row (it, row),
 25564           !NILP (overlay_arrow_string)))
 25565     {
 25566       /* Overlay arrow in window redisplay is a fringe bitmap.  */
 25567       if (STRINGP (overlay_arrow_string))
 25568         {
 25569           struct glyph_row *arrow_row
 25570             = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
 25571           struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
 25572           struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
 25573           struct glyph *p = row->glyphs[TEXT_AREA];
 25574           struct glyph *p2, *end;
 25575 
 25576           /* Copy the arrow glyphs.  */
 25577           while (glyph < arrow_end)
 25578             *p++ = *glyph++;
 25579 
 25580           /* Throw away padding glyphs.  */
 25581           p2 = p;
 25582           end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
 25583           while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
 25584             ++p2;
 25585           if (p2 > p)
 25586             {
 25587               while (p2 < end)
 25588                 *p++ = *p2++;
 25589               row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
 25590             }
 25591         }
 25592       else
 25593         {
 25594           eassert (FIXNUMP (overlay_arrow_string));
 25595           row->overlay_arrow_bitmap = XFIXNUM (overlay_arrow_string);
 25596         }
 25597       overlay_arrow_seen = true;
 25598     }
 25599 
 25600   /* Highlight trailing whitespace.  */
 25601   if (!NILP (Vshow_trailing_whitespace))
 25602     highlight_trailing_whitespace (it);
 25603 
 25604   /* Compute pixel dimensions of this line.  */
 25605   compute_line_metrics (it);
 25606 
 25607   /* Implementation note: No changes in the glyphs of ROW or in their
 25608      faces can be done past this point, because compute_line_metrics
 25609      computes ROW's hash value and stores it within the glyph_row
 25610      structure.  */
 25611 
 25612   /* Record whether this row ends inside an ellipsis.  */
 25613   row->ends_in_ellipsis_p
 25614     = (it->method == GET_FROM_DISPLAY_VECTOR
 25615        && it->ellipsis_p);
 25616 
 25617   /* Save fringe bitmaps in this row.  */
 25618   row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
 25619   row->left_user_fringe_face_id = it->left_user_fringe_face_id;
 25620   row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
 25621   row->right_user_fringe_face_id = it->right_user_fringe_face_id;
 25622 
 25623   it->left_user_fringe_bitmap = 0;
 25624   it->left_user_fringe_face_id = 0;
 25625   it->right_user_fringe_bitmap = 0;
 25626   it->right_user_fringe_face_id = 0;
 25627 
 25628   /* When they turn off tooltip-mode on a GUI frame, we call 'message'
 25629      with message-truncate-lines bound to non-nil, which produces
 25630      truncation bitmaps on the fringe.  Force redrawing of the fringes
 25631      in that case, to make sure the fringe bitmaps are removed when a
 25632      shorter message is displayed.  */
 25633   if (MINI_WINDOW_P (it->w) && it->line_wrap == TRUNCATE
 25634       && FRAME_WINDOW_P (it->f) && !cursor_in_echo_area)
 25635     row->redraw_fringe_bitmaps_p = true;
 25636 
 25637   /* Maybe set the cursor.  */
 25638   cvpos = it->w->cursor.vpos;
 25639   if ((cvpos < 0
 25640        /* In bidi-reordered rows, keep checking for proper cursor
 25641           position even if one has been found already, because buffer
 25642           positions in such rows change non-linearly with ROW->VPOS,
 25643           when a line is continued.  One exception: when we are at ZV,
 25644           display cursor on the first suitable glyph row, since all
 25645           the empty rows after that also have their position set to ZV.  */
 25646        /* FIXME: Revisit this when glyph ``spilling'' in continuation
 25647           lines' rows is implemented for bidi-reordered rows.  */
 25648        || (it->bidi_p
 25649            && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
 25650       && PT >= MATRIX_ROW_START_CHARPOS (row)
 25651       && PT <= MATRIX_ROW_END_CHARPOS (row)
 25652       && cursor_row_p (row))
 25653     set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
 25654 
 25655   /* Prepare for the next line.  This line starts horizontally at (X
 25656      HPOS) = (0 0).  Vertical positions are incremented.  As a
 25657      convenience for the caller, IT->glyph_row is set to the next
 25658      row to be used.  */
 25659   it->current_x = it->hpos = 0;
 25660   it->current_y += row->height;
 25661   /* Restore the first and last visible X if we adjusted them for
 25662      current-line hscrolling.  */
 25663   if (hscroll_this_line)
 25664     {
 25665       it->first_visible_x = first_visible_x;
 25666       it->last_visible_x  = last_visible_x;
 25667     }
 25668   SET_TEXT_POS (it->eol_pos, 0, 0);
 25669   ++it->vpos;
 25670   ++it->glyph_row;
 25671   /* The next row should by default use the same value of the
 25672      reversed_p flag as this one.  set_iterator_to_next decides when
 25673      it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
 25674      the flag accordingly.  */
 25675   if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
 25676     it->glyph_row->reversed_p = row->reversed_p;
 25677   it->start = row->end;
 25678   return MATRIX_ROW_DISPLAYS_TEXT_P (row);
 25679 
 25680 #undef RECORD_MAX_MIN_POS
 25681 }
 25682 
 25683 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
 25684        Scurrent_bidi_paragraph_direction, 0, 1, 0,
 25685        doc: /* Return paragraph direction at point in BUFFER.
 25686 Value is either `left-to-right' or `right-to-left'.
 25687 If BUFFER is omitted or nil, it defaults to the current buffer.
 25688 
 25689 Paragraph direction determines how the text in the paragraph is displayed.
 25690 In left-to-right paragraphs, text begins at the left margin of the window
 25691 and the reading direction is generally left to right.  In right-to-left
 25692 paragraphs, text begins at the right margin and is read from right to left.
 25693 
 25694 See also `bidi-paragraph-direction'.  */)
 25695   (Lisp_Object buffer)
 25696 {
 25697   struct buffer *buf = current_buffer;
 25698   struct buffer *old = buf;
 25699 
 25700   if (! NILP (buffer))
 25701     {
 25702       CHECK_BUFFER (buffer);
 25703       buf = XBUFFER (buffer);
 25704     }
 25705 
 25706   if (NILP (BVAR (buf, bidi_display_reordering))
 25707       || NILP (BVAR (buf, enable_multibyte_characters))
 25708       /* When we are loading loadup.el, the character property tables
 25709          needed for bidi iteration are not yet available.  */
 25710       || redisplay__inhibit_bidi)
 25711     return Qleft_to_right;
 25712   else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
 25713     return BVAR (buf, bidi_paragraph_direction);
 25714   else
 25715     {
 25716       /* Determine the direction from buffer text.  We could try to
 25717          use current_matrix if it is up to date, but this seems fast
 25718          enough as it is.  */
 25719       struct bidi_it itb;
 25720       ptrdiff_t pos = BUF_PT (buf);
 25721       ptrdiff_t bytepos = BUF_PT_BYTE (buf);
 25722       int c;
 25723       void *itb_data = bidi_shelve_cache ();
 25724 
 25725       set_buffer_temp (buf);
 25726       /* bidi_paragraph_init finds the base direction of the paragraph
 25727          by searching forward from paragraph start.  We need the base
 25728          direction of the current or _previous_ paragraph, so we need
 25729          to make sure we are within that paragraph.  To that end, find
 25730          the previous non-empty line.  */
 25731       if (pos >= ZV && pos > BEGV)
 25732         dec_both (&pos, &bytepos);
 25733       AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
 25734       if (fast_looking_at (trailing_white_space,
 25735                            pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
 25736         {
 25737           while ((c = FETCH_BYTE (bytepos)) == '\n'
 25738                  || c == ' ' || c == '\t' || c == '\f')
 25739             {
 25740               if (bytepos <= BEGV_BYTE)
 25741                 break;
 25742               bytepos--;
 25743               pos--;
 25744             }
 25745           while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
 25746             bytepos--;
 25747         }
 25748       bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
 25749       itb.paragraph_dir = NEUTRAL_DIR;
 25750       itb.string.s = NULL;
 25751       itb.string.lstring = Qnil;
 25752       itb.string.bufpos = 0;
 25753       itb.string.from_disp_str = false;
 25754       itb.string.unibyte = false;
 25755       /* We have no window to use here for ignoring window-specific
 25756          overlays.  Using NULL for window pointer will cause
 25757          compute_display_string_pos to use the current buffer.  */
 25758       itb.w = NULL;
 25759       bidi_paragraph_init (NEUTRAL_DIR, &itb, true);
 25760       bidi_unshelve_cache (itb_data, false);
 25761       set_buffer_temp (old);
 25762       switch (itb.paragraph_dir)
 25763         {
 25764         case L2R:
 25765           return Qleft_to_right;
 25766           break;
 25767         case R2L:
 25768           return Qright_to_left;
 25769           break;
 25770         default:
 25771           emacs_abort ();
 25772         }
 25773     }
 25774 }
 25775 
 25776 DEFUN ("bidi-find-overridden-directionality",
 25777        Fbidi_find_overridden_directionality,
 25778        Sbidi_find_overridden_directionality, 3, 4, 0,
 25779        doc: /* Return position between FROM and TO where directionality was overridden.
 25780 
 25781 This function returns the first character position in the specified
 25782 region of OBJECT where characters have their bidirectional
 25783 properties affected in a way that might make its text look confusingly
 25784 on display.  For example, characters whose `bidi-class' property is `L',
 25785 could be forced to display as `R' by a directional override, and
 25786 likewise characters whose `bidi-class' is `R' or `AL' that are
 25787 forced to display as `L'.
 25788 
 25789 If no such character is found, the function returns nil.
 25790 
 25791 OBJECT is a Lisp string or buffer to search for overridden
 25792 directionality, and defaults to the current buffer if nil.
 25793 OBJECT can also be a window, in which case the function will search
 25794 the buffer displayed in that window.  Passing the window instead of
 25795 a buffer is preferable when the buffer is displayed in some window,
 25796 because this function will then be able to correctly account for
 25797 window-specific overlays, which can affect the results.
 25798 
 25799 Optional argument BASE-DIR specifies the base paragraph directory
 25800 of the text.  It should be a symbol, either `left-to-right'
 25801 or `right-to-left', and defaults to `left-to-right'.
 25802 
 25803 Strong directional characters `L', `R', and `AL' can have their
 25804 intrinsic directionality overridden by directional override control
 25805 characters RLO (u+202E) and LRO (u+202D).  They can also have their
 25806 directionality affected by other formatting control characters: LRE
 25807 (u+202A), RLE (u+202B), LRI (u+2066), and RLI (u+2067).  See the
 25808 function `get-char-code-property' for a way to inquire about the
 25809 `bidi-class' property of a character.  Characters whose intrinsic
 25810 directionality is weak or neutral, such as numbers or punctuation
 25811 characters, can be forced to display in a very different place with
 25812 respect of its surrounding characters, so as to make the surrounding
 25813 text confuse the user regarding what the text says.
 25814 
 25815 Also see the `highlight-confusing-reorderings' function, which can be
 25816 useful in similar circumstances as this function.  */)
 25817   (Lisp_Object from, Lisp_Object to, Lisp_Object object, Lisp_Object base_dir)
 25818 {
 25819   struct buffer *buf = current_buffer;
 25820   struct buffer *old = buf;
 25821   struct window *w = NULL;
 25822   bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
 25823   struct bidi_it itb;
 25824   ptrdiff_t from_pos, to_pos, from_bpos;
 25825   void *itb_data;
 25826 
 25827   if (!NILP (object))
 25828     {
 25829       if (BUFFERP (object))
 25830         buf = XBUFFER (object);
 25831       else if (WINDOWP (object))
 25832         {
 25833           w = decode_live_window (object);
 25834           buf = XBUFFER (w->contents);
 25835           frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
 25836         }
 25837       else
 25838         CHECK_STRING (object);
 25839     }
 25840 
 25841   if (STRINGP (object))
 25842     {
 25843       /* Characters in unibyte strings are always treated by bidi.c as
 25844          strong LTR.  */
 25845       if (!STRING_MULTIBYTE (object)
 25846           /* When we are loading loadup.el, the character property
 25847              tables needed for bidi iteration are not yet
 25848              available.  */
 25849           || redisplay__inhibit_bidi)
 25850         return Qnil;
 25851 
 25852       validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
 25853       if (from_pos >= SCHARS (object))
 25854         return Qnil;
 25855 
 25856       /* Set up the bidi iterator.  */
 25857       itb_data = bidi_shelve_cache ();
 25858       itb.paragraph_dir = NEUTRAL_DIR;
 25859       itb.string.lstring = object;
 25860       itb.string.s = NULL;
 25861       itb.string.schars = SCHARS (object);
 25862       itb.string.bufpos = 0;
 25863       itb.string.from_disp_str = false;
 25864       itb.string.unibyte = false;
 25865       itb.w = w;
 25866       bidi_init_it (0, 0, frame_window_p, &itb);
 25867     }
 25868   else
 25869     {
 25870       /* Nothing this fancy can happen in unibyte buffers, or in a
 25871          buffer that disabled reordering, or if FROM is at EOB.  */
 25872       if (NILP (BVAR (buf, bidi_display_reordering))
 25873           || NILP (BVAR (buf, enable_multibyte_characters))
 25874           /* When we are loading loadup.el, the character property
 25875              tables needed for bidi iteration are not yet
 25876              available.  */
 25877           || redisplay__inhibit_bidi)
 25878         return Qnil;
 25879 
 25880       set_buffer_temp (buf);
 25881       validate_region (&from, &to);
 25882       from_pos = XFIXNUM (from);
 25883       to_pos = XFIXNUM (to);
 25884       if (from_pos >= ZV)
 25885         return Qnil;
 25886 
 25887       /* Set up the bidi iterator.  */
 25888       itb_data = bidi_shelve_cache ();
 25889       from_bpos = CHAR_TO_BYTE (from_pos);
 25890       if (from_pos == BEGV)
 25891         {
 25892           itb.charpos = BEGV;
 25893           itb.bytepos = BEGV_BYTE;
 25894         }
 25895       else if (FETCH_BYTE (from_bpos - 1) == '\n')
 25896         {
 25897           itb.charpos = from_pos;
 25898           itb.bytepos = from_bpos;
 25899         }
 25900       else
 25901         itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
 25902                                             -1, &itb.bytepos);
 25903       itb.paragraph_dir = NEUTRAL_DIR;
 25904       itb.string.s = NULL;
 25905       itb.string.lstring = Qnil;
 25906       itb.string.bufpos = 0;
 25907       itb.string.from_disp_str = false;
 25908       itb.string.unibyte = false;
 25909       itb.w = w;
 25910       bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
 25911     }
 25912 
 25913   ptrdiff_t found;
 25914   bidi_dir_t bdir = EQ (base_dir, Qright_to_left) ? R2L : L2R;
 25915   do {
 25916     bidi_paragraph_init (bdir, &itb, false);
 25917     while ((found = bidi_find_first_overridden (&itb)) < from_pos)
 25918       ;
 25919   } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
 25920 
 25921   bidi_unshelve_cache (itb_data, false);
 25922   set_buffer_temp (old);
 25923 
 25924   return (from_pos <= found && found < to_pos) ? make_fixnum (found) : Qnil;
 25925 }
 25926 
 25927 DEFUN ("move-point-visually", Fmove_point_visually,
 25928        Smove_point_visually, 1, 1, 0,
 25929        doc: /* Move point in the visual order in the specified DIRECTION.
 25930 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
 25931 left.
 25932 
 25933 Value is the new character position of point.  */)
 25934   (Lisp_Object direction)
 25935 {
 25936   struct window *w = XWINDOW (selected_window);
 25937   struct buffer *b = XBUFFER (w->contents);
 25938   struct glyph_row *row;
 25939   int dir;
 25940   Lisp_Object paragraph_dir;
 25941 
 25942 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)          \
 25943   (!(ROW)->continued_p                          \
 25944    && NILP ((GLYPH)->object)                    \
 25945    && (GLYPH)->type == CHAR_GLYPH               \
 25946    && (GLYPH)->u.ch == ' '                      \
 25947    && (GLYPH)->charpos >= 0                     \
 25948    && !(GLYPH)->avoid_cursor_p)
 25949 
 25950   CHECK_FIXNUM (direction);
 25951   dir = XFIXNUM (direction);
 25952   if (dir > 0)
 25953     dir = 1;
 25954   else
 25955     dir = -1;
 25956 
 25957   /* If current matrix is up-to-date, we can use the information
 25958      recorded in the glyphs, at least as long as the goal is on the
 25959      screen.  */
 25960   if (w->window_end_valid
 25961       && !windows_or_buffers_changed
 25962       && b
 25963       && !b->clip_changed
 25964       && !b->prevent_redisplay_optimizations_p
 25965       && !window_outdated (w)
 25966       /* We rely below on the cursor coordinates to be up to date, but
 25967          we cannot trust them if some command moved point since the
 25968          last complete redisplay.  */
 25969       && w->last_point == BUF_PT (b)
 25970       && w->cursor.vpos >= 0
 25971       && w->cursor.vpos < w->current_matrix->nrows
 25972       && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
 25973     {
 25974       struct glyph *g = row->glyphs[TEXT_AREA];
 25975       struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
 25976       struct glyph *gpt = g + w->cursor.hpos;
 25977 
 25978       for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
 25979         {
 25980           if (BUFFERP (g->object) && g->charpos != PT)
 25981             {
 25982               SET_PT (g->charpos);
 25983               w->cursor.vpos = -1;
 25984               return make_fixnum (PT);
 25985             }
 25986           else if (!NILP (g->object) && !EQ (g->object, gpt->object))
 25987             {
 25988               ptrdiff_t new_pos;
 25989 
 25990               if (BUFFERP (gpt->object))
 25991                 {
 25992                   new_pos = PT;
 25993                   if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
 25994                     new_pos += (row->reversed_p ? -dir : dir);
 25995                   else
 25996                     new_pos -= (row->reversed_p ? -dir : dir);
 25997                   new_pos = clip_to_bounds (BEGV, new_pos, ZV);
 25998                   /* If we didn't move, we've hit BEGV or ZV, so we
 25999                      need to signal a suitable error.  */
 26000                   if (new_pos == PT)
 26001                     break;
 26002                 }
 26003               else if (BUFFERP (g->object))
 26004                 new_pos = g->charpos;
 26005               else
 26006                 break;
 26007               SET_PT (new_pos);
 26008               w->cursor.vpos = -1;
 26009               return make_fixnum (PT);
 26010             }
 26011           else if (ROW_GLYPH_NEWLINE_P (row, g))
 26012             {
 26013               /* Glyphs inserted at the end of a non-empty line for
 26014                  positioning the cursor have zero charpos, so we must
 26015                  deduce the value of point by other means.  */
 26016               if (g->charpos > 0)
 26017                 SET_PT (g->charpos);
 26018               else if (row->ends_at_zv_p && PT != ZV)
 26019                 SET_PT (ZV);
 26020               else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
 26021                 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26022               else
 26023                 break;
 26024               w->cursor.vpos = -1;
 26025               return make_fixnum (PT);
 26026             }
 26027         }
 26028       if (g == e || NILP (g->object))
 26029         {
 26030           if (row->truncated_on_left_p || row->truncated_on_right_p)
 26031             goto simulate_display;
 26032           if (!row->reversed_p)
 26033             row += dir;
 26034           else
 26035             row -= dir;
 26036           if (!(MATRIX_FIRST_TEXT_ROW (w->current_matrix) <= row
 26037                 && row < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)))
 26038             goto simulate_display;
 26039 
 26040           if (dir > 0)
 26041             {
 26042               if (row->reversed_p && !row->continued_p)
 26043                 {
 26044                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26045                   w->cursor.vpos = -1;
 26046                   return make_fixnum (PT);
 26047                 }
 26048               g = row->glyphs[TEXT_AREA];
 26049               e = g + row->used[TEXT_AREA];
 26050               for ( ; g < e; g++)
 26051                 {
 26052                   if (BUFFERP (g->object)
 26053                       /* Empty lines have only one glyph, which stands
 26054                          for the newline, and whose charpos is the
 26055                          buffer position of the newline.  */
 26056                       || ROW_GLYPH_NEWLINE_P (row, g)
 26057                       /* When the buffer ends in a newline, the line at
 26058                          EOB also has one glyph, but its charpos is -1.  */
 26059                       || (row->ends_at_zv_p
 26060                           && !row->reversed_p
 26061                           && NILP (g->object)
 26062                           && g->type == CHAR_GLYPH
 26063                           && g->u.ch == ' '))
 26064                     {
 26065                       if (g->charpos > 0)
 26066                         SET_PT (g->charpos);
 26067                       else if (!row->reversed_p
 26068                                && row->ends_at_zv_p
 26069                                && PT != ZV)
 26070                         SET_PT (ZV);
 26071                       else
 26072                         continue;
 26073                       w->cursor.vpos = -1;
 26074                       return make_fixnum (PT);
 26075                     }
 26076                 }
 26077             }
 26078           else
 26079             {
 26080               if (!row->reversed_p && !row->continued_p)
 26081                 {
 26082                   SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
 26083                   w->cursor.vpos = -1;
 26084                   return make_fixnum (PT);
 26085                 }
 26086               e = row->glyphs[TEXT_AREA];
 26087               g = e + row->used[TEXT_AREA] - 1;
 26088               for ( ; g >= e; g--)
 26089                 {
 26090                   if (BUFFERP (g->object)
 26091                       || (ROW_GLYPH_NEWLINE_P (row, g)
 26092                           && g->charpos > 0)
 26093                       /* Empty R2L lines on GUI frames have the buffer
 26094                          position of the newline stored in the stretch
 26095                          glyph.  */
 26096                       || g->type == STRETCH_GLYPH
 26097                       || (row->ends_at_zv_p
 26098                           && row->reversed_p
 26099                           && NILP (g->object)
 26100                           && g->type == CHAR_GLYPH
 26101                           && g->u.ch == ' '))
 26102                     {
 26103                       if (g->charpos > 0)
 26104                         SET_PT (g->charpos);
 26105                       else if (row->reversed_p
 26106                                && row->ends_at_zv_p
 26107                                && PT != ZV)
 26108                         SET_PT (ZV);
 26109                       else
 26110                         continue;
 26111                       w->cursor.vpos = -1;
 26112                       return make_fixnum (PT);
 26113                     }
 26114                 }
 26115             }
 26116         }
 26117     }
 26118 
 26119  simulate_display:
 26120 
 26121   /* If we wind up here, we failed to move by using the glyphs, so we
 26122      need to simulate display instead.  */
 26123 
 26124   if (b)
 26125     paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
 26126   else
 26127     paragraph_dir = Qleft_to_right;
 26128   if (EQ (paragraph_dir, Qright_to_left))
 26129     dir = -dir;
 26130   if (PT <= BEGV && dir < 0)
 26131     xsignal0 (Qbeginning_of_buffer);
 26132   else if (PT >= ZV && dir > 0)
 26133     xsignal0 (Qend_of_buffer);
 26134   else
 26135     {
 26136       struct text_pos pt;
 26137       struct it it;
 26138       int pt_x, target_x, pixel_width, pt_vpos;
 26139       bool at_eol_p;
 26140       bool overshoot_expected = false;
 26141       bool target_is_eol_p = false;
 26142       void *itdata = bidi_shelve_cache ();
 26143 
 26144       /* Setup the arena.  */
 26145       SET_TEXT_POS (pt, PT, PT_BYTE);
 26146       start_display (&it, w, pt);
 26147       /* When lines are truncated, we could be called with point
 26148          outside of the windows edges, in which case move_it_*
 26149          functions either prematurely stop at window's edge or jump to
 26150          the next screen line, whereas we rely below on our ability to
 26151          reach point, in order to start from its X coordinate.  So we
 26152          need to disregard the window's horizontal extent in that case.  */
 26153       if (it.line_wrap == TRUNCATE)
 26154         it.last_visible_x = DISP_INFINITY;
 26155 
 26156       if (it.cmp_it.id < 0
 26157           && it.method == GET_FROM_STRING
 26158           && it.area == TEXT_AREA
 26159           && it.string_from_display_prop_p
 26160           && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
 26161         overshoot_expected = true;
 26162 
 26163       /* Find the X coordinate of point.  We start from the beginning
 26164          of this or previous line to make sure we are before point in
 26165          the logical order (since the move_it_* functions can only
 26166          move forward).  */
 26167     reseat:
 26168       reseat_at_previous_visible_line_start (&it);
 26169       it.current_x = it.hpos = it.current_y = it.vpos = 0;
 26170       if (IT_CHARPOS (it) != PT)
 26171         {
 26172           move_it_to (&it, overshoot_expected ? PT - 1 : PT,
 26173                       -1, -1, -1, MOVE_TO_POS);
 26174           /* If we missed point because the character there is
 26175              displayed out of a display vector that has more than one
 26176              glyph, retry expecting overshoot.  */
 26177           if (it.method == GET_FROM_DISPLAY_VECTOR
 26178               && it.current.dpvec_index > 0
 26179               && !overshoot_expected)
 26180             {
 26181               overshoot_expected = true;
 26182               goto reseat;
 26183             }
 26184           else if (IT_CHARPOS (it) != PT && !overshoot_expected)
 26185             move_it_in_display_line (&it, PT, -1, MOVE_TO_POS);
 26186         }
 26187       pt_x = it.current_x;
 26188       pt_vpos = it.vpos;
 26189       if (dir > 0 || overshoot_expected)
 26190         {
 26191           struct glyph_row *row = it.glyph_row;
 26192 
 26193           /* When point is at beginning of line, we don't have
 26194              information about the glyph there loaded into struct
 26195              it.  Calling get_next_display_element fixes that.  */
 26196           if (pt_x == 0)
 26197             get_next_display_element (&it);
 26198           at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26199           it.glyph_row = NULL;
 26200           PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
 26201           it.glyph_row = row;
 26202           /* PRODUCE_GLYPHS advances it.current_x, so we must restore
 26203              it, lest it will become out of sync with it's buffer
 26204              position.  */
 26205           it.current_x = pt_x;
 26206         }
 26207       else
 26208         at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
 26209       pixel_width = it.pixel_width;
 26210       if (overshoot_expected && at_eol_p)
 26211         pixel_width = 0;
 26212       else if (pixel_width <= 0)
 26213         pixel_width = 1;
 26214 
 26215       /* If there's a display string (or something similar) at point,
 26216          we are actually at the glyph to the left of point, so we need
 26217          to correct the X coordinate.  */
 26218       if (overshoot_expected)
 26219         {
 26220           if (it.bidi_p)
 26221             pt_x += pixel_width * it.bidi_it.scan_dir;
 26222           else
 26223             pt_x += pixel_width;
 26224         }
 26225 
 26226       /* Compute target X coordinate, either to the left or to the
 26227          right of point.  On TTY frames, all characters have the same
 26228          pixel width of 1, so we can use that.  On GUI frames we don't
 26229          have an easy way of getting at the pixel width of the
 26230          character to the left of point, so we use a different method
 26231          of getting to that place.  */
 26232       if (dir > 0)
 26233         target_x = pt_x + pixel_width;
 26234       else
 26235         target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
 26236 
 26237       /* Target X coordinate could be one line above or below the line
 26238          of point, in which case we need to adjust the target X
 26239          coordinate.  Also, if moving to the left, we need to begin at
 26240          the left edge of the point's screen line.  */
 26241       if (dir < 0)
 26242         {
 26243           if (pt_x > 0)
 26244             {
 26245               start_display (&it, w, pt);
 26246               if (it.line_wrap == TRUNCATE)
 26247                 it.last_visible_x = DISP_INFINITY;
 26248               reseat_at_previous_visible_line_start (&it);
 26249               it.current_x = it.current_y = it.hpos = 0;
 26250               if (pt_vpos != 0)
 26251                 move_it_by_lines (&it, pt_vpos);
 26252             }
 26253           else
 26254             {
 26255               move_it_by_lines (&it, -1);
 26256               target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
 26257               target_is_eol_p = true;
 26258               /* Under word-wrap, we don't know the x coordinate of
 26259                  the last character displayed on the previous line,
 26260                  which immediately precedes the wrap point.  To find
 26261                  out its x coordinate, we try moving to the right
 26262                  margin of the window, which will stop at the wrap
 26263                  point, and then reset target_x to point at the
 26264                  character that precedes the wrap point.  This is not
 26265                  needed on GUI frames, because (see below) there we
 26266                  move from the left margin one grapheme cluster at a
 26267                  time, and stop when we hit the wrap point.  */
 26268               if (!FRAME_WINDOW_P (it.f) && it.line_wrap == WORD_WRAP)
 26269                 {
 26270                   void *it_data = NULL;
 26271                   struct it it2;
 26272 
 26273                   SAVE_IT (it2, it, it_data);
 26274                   move_it_in_display_line_to (&it, ZV, target_x,
 26275                                               MOVE_TO_POS | MOVE_TO_X);
 26276                   /* If we arrived at target_x, that _is_ the last
 26277                      character on the previous line.  */
 26278                   if (it.current_x != target_x)
 26279                     target_x = it.current_x - 1;
 26280                   RESTORE_IT (&it, &it2, it_data);
 26281                 }
 26282             }
 26283         }
 26284       else
 26285         {
 26286           if (at_eol_p
 26287               || (target_x >= it.last_visible_x
 26288                   && it.line_wrap != TRUNCATE))
 26289             {
 26290               if (pt_x > 0)
 26291                 move_it_by_lines (&it, 0);
 26292               move_it_by_lines (&it, 1);
 26293               target_x = 0;
 26294             }
 26295         }
 26296 
 26297       /* Move to the target X coordinate.  */
 26298       /* On GUI frames, as we don't know the X coordinate of the
 26299          character to the left of point, moving point to the left
 26300          requires walking, one grapheme cluster at a time, until we
 26301          find ourself at a place immediately to the left of the
 26302          character at point.  */
 26303       if (FRAME_WINDOW_P (it.f) && dir < 0)
 26304         {
 26305           struct text_pos new_pos;
 26306           enum move_it_result rc = MOVE_X_REACHED;
 26307 
 26308           if (it.current_x == 0)
 26309             get_next_display_element (&it);
 26310           if (it.what == IT_COMPOSITION)
 26311             {
 26312               new_pos.charpos = it.cmp_it.charpos;
 26313               new_pos.bytepos = -1;
 26314             }
 26315           else
 26316             new_pos = it.current.pos;
 26317 
 26318           while (it.current_x + it.pixel_width <= target_x
 26319                  && (rc == MOVE_X_REACHED
 26320                      /* Under word-wrap, move_it_in_display_line_to
 26321                         stops at correct coordinates, but sometimes
 26322                         returns MOVE_POS_MATCH_OR_ZV.  */
 26323                      || (it.line_wrap == WORD_WRAP
 26324                          && rc == MOVE_POS_MATCH_OR_ZV)))
 26325             {
 26326               int new_x = it.current_x + it.pixel_width;
 26327 
 26328               /* For composed characters, we want the position of the
 26329                  first character in the grapheme cluster (usually, the
 26330                  composition's base character), whereas it.current
 26331                  might give us the position of the _last_ one, e.g. if
 26332                  the composition is rendered in reverse due to bidi
 26333                  reordering.  */
 26334               if (it.what == IT_COMPOSITION)
 26335                 {
 26336                   new_pos.charpos = it.cmp_it.charpos;
 26337                   new_pos.bytepos = -1;
 26338                 }
 26339               else
 26340                 new_pos = it.current.pos;
 26341               if (new_x == it.current_x)
 26342                 new_x++;
 26343               rc = move_it_in_display_line_to (&it, ZV, new_x,
 26344                                                MOVE_TO_POS | MOVE_TO_X);
 26345               if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
 26346                 break;
 26347             }
 26348           /* The previous position we saw in the loop is the one we
 26349              want.  */
 26350           if (new_pos.bytepos == -1)
 26351             new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
 26352           it.current.pos = new_pos;
 26353         }
 26354       else if (it.current_x != target_x)
 26355         move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
 26356 
 26357       /* If we ended up in a display string that covers point, move to
 26358          buffer position to the right in the visual order.  */
 26359       if (dir > 0)
 26360         {
 26361           while (IT_CHARPOS (it) == PT)
 26362             {
 26363               set_iterator_to_next (&it, false);
 26364               if (!get_next_display_element (&it))
 26365                 break;
 26366             }
 26367         }
 26368 
 26369       /* Move point to that position.  */
 26370       SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
 26371       bidi_unshelve_cache (itdata, false);
 26372     }
 26373 
 26374   return make_fixnum (PT);
 26375 
 26376 #undef ROW_GLYPH_NEWLINE_P
 26377 }
 26378 
 26379 DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
 26380        Sbidi_resolved_levels, 0, 1, 0,
 26381        doc: /* Return the resolved bidirectional levels of characters at VPOS.
 26382 
 26383 The resolved levels are produced by the Emacs bidi reordering engine
 26384 that implements the UBA, the Unicode Bidirectional Algorithm.  Please
 26385 read the Unicode Standard Annex 9 (UAX#9) for background information
 26386 about these levels.
 26387 
 26388 VPOS is the zero-based number of the current window's screen line
 26389 for which to produce the resolved levels.  If VPOS is nil or omitted,
 26390 it defaults to the screen line of point.  If the window displays a
 26391 header line, VPOS of zero will report on the header line, and first
 26392 line of text in the window will have VPOS of 1.
 26393 
 26394 Value is an array of resolved levels, indexed by glyph number.
 26395 Glyphs are numbered from zero starting from the beginning of the
 26396 screen line, i.e. the left edge of the window for left-to-right lines
 26397 and from the right edge for right-to-left lines.  The resolved levels
 26398 are produced only for the window's text area; text in display margins
 26399 is not included.
 26400 
 26401 If the selected window's display is not up-to-date, or if the specified
 26402 screen line does not display text, this function returns nil.  It is
 26403 highly recommended to bind this function to some simple key, like F8,
 26404 in order to avoid these problems.
 26405 
 26406 This function exists mainly for testing the correctness of the
 26407 Emacs UBA implementation, in particular with the test suite.  */)
 26408   (Lisp_Object vpos)
 26409 {
 26410   struct window *w = XWINDOW (selected_window);
 26411   struct buffer *b = XBUFFER (w->contents);
 26412   int nrow;
 26413   struct glyph_row *row;
 26414 
 26415   if (NILP (vpos))
 26416     {
 26417       int d1, d2, d3, d4, d5;
 26418 
 26419       pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
 26420     }
 26421   else
 26422     {
 26423       CHECK_FIXNUM (vpos);
 26424       nrow = XFIXNUM (vpos);
 26425     }
 26426 
 26427   /* We require up-to-date glyph matrix for this window.  */
 26428   if (w->window_end_valid
 26429       && !windows_or_buffers_changed
 26430       && b
 26431       && !b->clip_changed
 26432       && !b->prevent_redisplay_optimizations_p
 26433       && !window_outdated (w)
 26434       && nrow >= 0
 26435       && nrow < w->current_matrix->nrows
 26436       && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
 26437       && MATRIX_ROW_DISPLAYS_TEXT_P (row))
 26438     {
 26439       struct glyph *g, *e, *g1;
 26440       int nglyphs, i;
 26441       Lisp_Object levels;
 26442 
 26443       if (!row->reversed_p)     /* Left-to-right glyph row.  */
 26444         {
 26445           g = g1 = row->glyphs[TEXT_AREA];
 26446           e = g + row->used[TEXT_AREA];
 26447 
 26448           /* Skip over glyphs at the start of the row that was
 26449              generated by redisplay for its own needs.  */
 26450           while (g < e
 26451                  && NILP (g->object)
 26452                  && g->charpos < 0)
 26453             g++;
 26454           g1 = g;
 26455 
 26456           /* Count the "interesting" glyphs in this row.  */
 26457           for (nglyphs = 0; g < e && !NILP (g->object); g++)
 26458             nglyphs++;
 26459 
 26460           /* Create and fill the array.  */
 26461           levels = make_uninit_vector (nglyphs);
 26462           for (i = 0; g1 < g; i++, g1++)
 26463             ASET (levels, i, make_fixnum (g1->resolved_level));
 26464         }
 26465       else                      /* Right-to-left glyph row.  */
 26466         {
 26467           g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 26468           e = row->glyphs[TEXT_AREA] - 1;
 26469           while (g > e
 26470                  && NILP (g->object)
 26471                  && g->charpos < 0)
 26472             g--;
 26473           g1 = g;
 26474           for (nglyphs = 0; g > e && !NILP (g->object); g--)
 26475             nglyphs++;
 26476           levels = make_uninit_vector (nglyphs);
 26477           for (i = 0; g1 > g; i++, g1--)
 26478             ASET (levels, i, make_fixnum (g1->resolved_level));
 26479         }
 26480       return levels;
 26481     }
 26482   else
 26483     return Qnil;
 26484 }
 26485 
 26486 
 26487 
 26488 /***********************************************************************
 26489                                Menu Bar
 26490  ***********************************************************************/
 26491 
 26492 /* Redisplay the menu bar in the frame for window W.
 26493 
 26494    The menu bar of X frames that don't have X toolkit support is
 26495    displayed in a special window W->frame->menu_bar_window.
 26496 
 26497    The menu bar of terminal frames is treated specially as far as
 26498    glyph matrices are concerned.  Menu bar lines are not part of
 26499    windows, so the update is done directly on the frame matrix rows
 26500    for the menu bar.  */
 26501 
 26502 static void
 26503 display_menu_bar (struct window *w)
 26504 {
 26505   struct frame *f = XFRAME (WINDOW_FRAME (w));
 26506   struct it it;
 26507   Lisp_Object items;
 26508   int i;
 26509 
 26510   /* Don't do all this for graphical frames.  */
 26511 #ifdef HAVE_NTGUI
 26512   if (FRAME_W32_P (f))
 26513     return;
 26514 #endif
 26515 #if defined (HAVE_PGTK)
 26516   if (FRAME_PGTK_P (f))
 26517     return;
 26518 #endif
 26519 
 26520 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26521   if (FRAME_X_P (f))
 26522     return;
 26523 #endif
 26524 
 26525 #ifdef HAVE_NS
 26526   if (FRAME_NS_P (f))
 26527     return;
 26528 #endif /* HAVE_NS */
 26529 
 26530 #ifdef HAVE_HAIKU
 26531   if (FRAME_HAIKU_P (f))
 26532     return;
 26533 #endif /* HAVE_HAIKU */
 26534 
 26535 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
 26536   eassert (!FRAME_WINDOW_P (f));
 26537   init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
 26538   it.first_visible_x = 0;
 26539   it.last_visible_x = FRAME_PIXEL_WIDTH (f);
 26540 #elif defined (HAVE_X_WINDOWS) /* X without toolkit.  */
 26541   struct window *menu_window = NULL;
 26542   struct face *face = FACE_FROM_ID (f, MENU_FACE_ID);
 26543 
 26544   if (FRAME_WINDOW_P (f))
 26545     {
 26546       /* Menu bar lines are displayed in the desired matrix of the
 26547          dummy window menu_bar_window.  */
 26548       menu_window = XWINDOW (f->menu_bar_window);
 26549       init_iterator (&it, menu_window, -1, -1,
 26550                      menu_window->desired_matrix->rows,
 26551                      MENU_FACE_ID);
 26552     }
 26553   else
 26554 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 26555     {
 26556       /* This is a TTY frame, i.e. character hpos/vpos are used as
 26557          pixel x/y.  */
 26558       init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
 26559                      MENU_FACE_ID);
 26560       it.first_visible_x = 0;
 26561       it.last_visible_x = FRAME_COLS (f);
 26562     }
 26563 
 26564   /* FIXME: This should be controlled by a user option.  See the
 26565      comments in redisplay_tool_bar and display_mode_line about
 26566      this.  */
 26567   it.paragraph_embedding = L2R;
 26568 
 26569   /* Clear all rows of the menu bar.  */
 26570   for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
 26571     {
 26572       struct glyph_row *row = it.glyph_row + i;
 26573       clear_glyph_row (row);
 26574       row->enabled_p = true;
 26575       row->full_width_p = true;
 26576       row->reversed_p = false;
 26577     }
 26578 
 26579   /* Display all items of the menu bar.  */
 26580   items = FRAME_MENU_BAR_ITEMS (it.f);
 26581   for (i = 0; i < ASIZE (items); i += 4)
 26582     {
 26583       Lisp_Object string;
 26584 
 26585       /* Stop at nil string.  */
 26586       string = AREF (items, i + 1);
 26587       if (NILP (string))
 26588         break;
 26589 
 26590       /* Remember where item was displayed.  */
 26591       ASET (items, i + 3, make_fixnum (it.hpos));
 26592 
 26593       /* Display the item, pad with one space.  */
 26594       if (it.current_x < it.last_visible_x)
 26595         display_string (NULL, string, Qnil, 0, 0, &it,
 26596                         SCHARS (string) + 1, 0, 0, STRING_MULTIBYTE (string));
 26597     }
 26598 
 26599   /* Fill out the line with spaces.  */
 26600   if (it.current_x < it.last_visible_x)
 26601     display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
 26602 
 26603   /* Compute the total height of the lines.  */
 26604   compute_line_metrics (&it);
 26605   it.glyph_row->full_width_p = true;
 26606   it.glyph_row->continued_p = false;
 26607   it.glyph_row->truncated_on_left_p = false;
 26608   it.glyph_row->truncated_on_right_p = false;
 26609 
 26610 #if defined (HAVE_X_WINDOWS) && !defined (USE_X_TOOLKIT) && !defined (USE_GTK)
 26611   /* Make a 3D menu bar have a shadow at its right end.  */
 26612   extend_face_to_end_of_line (&it);
 26613   if (face->box != FACE_NO_BOX)
 26614     {
 26615       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 26616                             + it.glyph_row->used[TEXT_AREA] - 1);
 26617       int box_thickness = face->box_vertical_line_width;
 26618       last->right_box_line_p = true;
 26619       /* Add back the space for the right box line we subtracted in
 26620          init_iterator, since the right_box_line_p flag will make the
 26621          glyph wider.  We actually add only as much space as is
 26622          available for the last glyph of the menu bar and whatever
 26623          space is left beyond it, since that glyph could be only
 26624          partially visible.  */
 26625       if (box_thickness > 0)
 26626         last->pixel_width += max (0, (box_thickness
 26627                                       - (it.current_x - it.last_visible_x)));
 26628     }
 26629 
 26630   /* With the non-toolkit version, modify the menu bar window height
 26631      accordingly.  */
 26632   if (FRAME_WINDOW_P (it.f) && menu_window)
 26633     {
 26634       struct glyph_row *row;
 26635       int delta_height;
 26636 
 26637       row = it.glyph_row;
 26638       delta_height
 26639         = ((row->y + row->height)
 26640            - WINDOW_BOX_HEIGHT_NO_MODE_LINE (menu_window));
 26641 
 26642       if (delta_height != 0)
 26643         {
 26644           FRAME_MENU_BAR_HEIGHT (it.f) += delta_height;
 26645           adjust_frame_size (it.f, -1, -1, 3, false, Qmenu_bar_lines);
 26646         }
 26647     }
 26648 #endif
 26649 }
 26650 
 26651 /* Deep copy of a glyph row, including the glyphs.  */
 26652 static void
 26653 deep_copy_glyph_row (struct glyph_row *to, struct glyph_row *from)
 26654 {
 26655   struct glyph *pointers[1 + LAST_AREA];
 26656   int to_used = to->used[TEXT_AREA];
 26657 
 26658   /* Save glyph pointers of TO.  */
 26659   memcpy (pointers, to->glyphs, sizeof to->glyphs);
 26660 
 26661   /* Do a structure assignment.  */
 26662   *to = *from;
 26663 
 26664   /* Restore original glyph pointers of TO.  */
 26665   memcpy (to->glyphs, pointers, sizeof to->glyphs);
 26666 
 26667   /* Copy the glyphs.  */
 26668   memcpy (to->glyphs[TEXT_AREA], from->glyphs[TEXT_AREA],
 26669           min (from->used[TEXT_AREA], to_used) * sizeof (struct glyph));
 26670 
 26671   /* If we filled only part of the TO row, fill the rest with
 26672      space_glyph (which will display as empty space).  */
 26673   if (to_used > from->used[TEXT_AREA])
 26674     fill_up_frame_row_with_spaces (to, to_used);
 26675 }
 26676 
 26677 /* Display one menu item on a TTY, by overwriting the glyphs in the
 26678    frame F's desired glyph matrix with glyphs produced from the menu
 26679    item text.  Called from term.c to display TTY drop-down menus one
 26680    item at a time.
 26681 
 26682    ITEM_TEXT is the menu item text as a C string.
 26683 
 26684    FACE_ID is the face ID to be used for this menu item.  FACE_ID
 26685    could specify one of 3 faces: a face for an enabled item, a face
 26686    for a disabled item, or a face for a selected item.
 26687 
 26688    X and Y are coordinates of the first glyph in the frame's desired
 26689    matrix to be overwritten by the menu item.  Since this is a TTY, Y
 26690    is the zero-based number of the glyph row and X is the zero-based
 26691    glyph number in the row, starting from left, where to start
 26692    displaying the item.
 26693 
 26694    SUBMENU means this menu item drops down a submenu, which
 26695    should be indicated by displaying a proper visual cue after the
 26696    item text.  */
 26697 
 26698 void
 26699 display_tty_menu_item (const char *item_text, int width, int face_id,
 26700                        int x, int y, bool submenu)
 26701 {
 26702   struct it it;
 26703   struct frame *f = SELECTED_FRAME ();
 26704   struct window *w = XWINDOW (f->selected_window);
 26705   struct glyph_row *row;
 26706   size_t item_len = strlen (item_text);
 26707 
 26708   eassert (FRAME_TERMCAP_P (f));
 26709 
 26710   /* Don't write beyond the matrix's last row.  This can happen for
 26711      TTY screens that are not high enough to show the entire menu.
 26712      (This is actually a bit of defensive programming, as
 26713      tty_menu_display already limits the number of menu items to one
 26714      less than the number of screen lines.)  */
 26715   if (y >= f->desired_matrix->nrows)
 26716     return;
 26717 
 26718   init_iterator (&it, w, -1, -1, f->desired_matrix->rows + y, MENU_FACE_ID);
 26719   it.first_visible_x = 0;
 26720   it.last_visible_x = FRAME_COLS (f) - 1;
 26721   row = it.glyph_row;
 26722   /* Start with the row contents from the current matrix.  */
 26723   deep_copy_glyph_row (row, f->current_matrix->rows + y);
 26724   bool saved_width = row->full_width_p;
 26725   row->full_width_p = true;
 26726   bool saved_reversed = row->reversed_p;
 26727   row->reversed_p = false;
 26728   row->enabled_p = true;
 26729 
 26730   /* Arrange for the menu item glyphs to start at (X,Y) and have the
 26731      desired face.  */
 26732   eassert (x < f->desired_matrix->matrix_w);
 26733   it.current_x = it.hpos = x;
 26734   it.current_y = it.vpos = y;
 26735   int saved_used = row->used[TEXT_AREA];
 26736   bool saved_truncated = row->truncated_on_right_p;
 26737   row->used[TEXT_AREA] = x;
 26738   it.face_id = face_id;
 26739   it.line_wrap = TRUNCATE;
 26740 
 26741   /* FIXME: This should be controlled by a user option.  See the
 26742      comments in redisplay_tool_bar and display_mode_line about this.
 26743      Also, if paragraph_embedding could ever be R2L, changes will be
 26744      needed to avoid shifting to the right the row characters in
 26745      term.c:append_glyph.  */
 26746   it.paragraph_embedding = L2R;
 26747 
 26748   /* Pad with a space on the left.  */
 26749   display_string (" ", Qnil, Qnil, 0, 0, &it, 1, 0, FRAME_COLS (f) - 1, -1);
 26750   width--;
 26751   /* Display the menu item, pad with spaces to WIDTH.  */
 26752   if (submenu)
 26753     {
 26754       display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26755                       item_len, 0, FRAME_COLS (f) - 1, -1);
 26756       width -= item_len;
 26757       /* Indicate with " >" that there's a submenu.  */
 26758       display_string (" >", Qnil, Qnil, 0, 0, &it, width, 0,
 26759                       FRAME_COLS (f) - 1, -1);
 26760     }
 26761   else
 26762     display_string (item_text, Qnil, Qnil, 0, 0, &it,
 26763                     width, 0, FRAME_COLS (f) - 1, -1);
 26764 
 26765   row->used[TEXT_AREA] = max (saved_used, row->used[TEXT_AREA]);
 26766   row->truncated_on_right_p = saved_truncated;
 26767   row->hash = row_hash (row);
 26768   row->full_width_p = saved_width;
 26769   row->reversed_p = saved_reversed;
 26770 }
 26771 
 26772 /***********************************************************************
 26773                               Mode Line
 26774  ***********************************************************************/
 26775 
 26776 /* Redisplay mode lines in the window tree whose root is WINDOW.
 26777    If FORCE, redisplay mode lines unconditionally.
 26778    Otherwise, redisplay only mode lines that are garbaged.  Value is
 26779    the number of windows whose mode lines were redisplayed.  */
 26780 
 26781 static int
 26782 redisplay_mode_lines (Lisp_Object window, bool force)
 26783 {
 26784   int nwindows = 0;
 26785 
 26786   while (!NILP (window))
 26787     {
 26788       struct window *w = XWINDOW (window);
 26789 
 26790       if (WINDOWP (w->contents))
 26791         nwindows += redisplay_mode_lines (w->contents, force);
 26792       else if (force
 26793                || FRAME_GARBAGED_P (XFRAME (w->frame))
 26794                || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
 26795         {
 26796           struct text_pos lpoint;
 26797           struct buffer *old = current_buffer;
 26798 
 26799           /* Set the window's buffer for the mode line display.  */
 26800           SET_TEXT_POS (lpoint, PT, PT_BYTE);
 26801           set_buffer_internal_1 (XBUFFER (w->contents));
 26802 
 26803           /* Point refers normally to the selected window.  For any
 26804              other window, set up appropriate value.  */
 26805           if (!EQ (window, selected_window))
 26806             {
 26807               struct text_pos pt;
 26808 
 26809               CLIP_TEXT_POS_FROM_MARKER (pt, w->pointm);
 26810               TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
 26811             }
 26812 
 26813           /* Display mode lines.  */
 26814           clear_glyph_matrix (w->desired_matrix);
 26815           if (display_mode_lines (w))
 26816             ++nwindows;
 26817 
 26818           /* Restore old settings.  */
 26819           set_buffer_internal_1 (old);
 26820           TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
 26821         }
 26822 
 26823       window = w->next;
 26824     }
 26825 
 26826   return nwindows;
 26827 }
 26828 
 26829 
 26830 /* Display the mode line, the header line, and the tab-line of window
 26831    W.  Value is the sum number of mode lines, header lines, and tab
 26832    lines actually displayed.  */
 26833 
 26834 static int
 26835 display_mode_lines (struct window *w)
 26836 {
 26837   Lisp_Object old_selected_window = selected_window;
 26838   Lisp_Object new_frame = w->frame;
 26839   specpdl_ref count = SPECPDL_INDEX ();
 26840   int n = 0;
 26841 
 26842   record_unwind_protect (restore_selected_window, selected_window);
 26843   record_unwind_protect
 26844     (restore_frame_selected_window, XFRAME (new_frame)->selected_window);
 26845 
 26846   if (window_wants_mode_line (w))
 26847     {
 26848       Lisp_Object window;
 26849       Lisp_Object default_help
 26850         = buffer_local_value (Qmode_line_default_help_echo, w->contents);
 26851 
 26852       /* Set up mode line help echo.  Do this before selecting w so it
 26853          can reasonably tell whether a mouse click will select w.  */
 26854       XSETWINDOW (window, w);
 26855       if (FUNCTIONP (default_help))
 26856         wset_mode_line_help_echo (w, safe_call1 (default_help, window));
 26857       else if (STRINGP (default_help))
 26858         wset_mode_line_help_echo (w, default_help);
 26859       else
 26860         wset_mode_line_help_echo (w, Qnil);
 26861     }
 26862 
 26863   selected_frame = new_frame;
 26864   /* FIXME: If we were to allow the mode-line's computation changing the buffer
 26865      or window's point, then we'd need select_window_1 here as well.  */
 26866   XSETWINDOW (selected_window, w);
 26867   XFRAME (new_frame)->selected_window = selected_window;
 26868 
 26869   /* These will be set while the mode line specs are processed.  */
 26870   line_number_displayed = false;
 26871   w->column_number_displayed = -1;
 26872 
 26873   if (window_wants_mode_line (w))
 26874     {
 26875       Lisp_Object window_mode_line_format
 26876         = window_parameter (w, Qmode_line_format);
 26877       struct window *sel_w = XWINDOW (old_selected_window);
 26878 
 26879       /* Select mode line face based on the real selected window.  */
 26880       display_mode_line (w,
 26881                          CURRENT_MODE_LINE_ACTIVE_FACE_ID_3 (sel_w, sel_w, w),
 26882                          NILP (window_mode_line_format)
 26883                          ? BVAR (current_buffer, mode_line_format)
 26884                          : window_mode_line_format);
 26885       ++n;
 26886     }
 26887 
 26888   if (window_wants_tab_line (w))
 26889     {
 26890       Lisp_Object window_tab_line_format
 26891         = window_parameter (w, Qtab_line_format);
 26892 
 26893       display_mode_line (w, TAB_LINE_FACE_ID,
 26894                          NILP (window_tab_line_format)
 26895                          ? BVAR (current_buffer, tab_line_format)
 26896                          : window_tab_line_format);
 26897       ++n;
 26898     }
 26899 
 26900   if (window_wants_header_line (w))
 26901     {
 26902       Lisp_Object window_header_line_format
 26903         = window_parameter (w, Qheader_line_format);
 26904 
 26905       display_mode_line (w, HEADER_LINE_FACE_ID,
 26906                          NILP (window_header_line_format)
 26907                          ? BVAR (current_buffer, header_line_format)
 26908                          : window_header_line_format);
 26909       ++n;
 26910     }
 26911 
 26912   unbind_to (count, Qnil);
 26913 
 26914   if (n > 0)
 26915     w->must_be_updated_p = true;
 26916   return n;
 26917 }
 26918 
 26919 
 26920 /* Display mode or header/tab line of window W.  FACE_ID specifies
 26921    which line to display; it is either MODE_LINE_ACTIVE_FACE_ID,
 26922    HEADER_LINE_FACE_ID or TAB_LINE_FACE_ID.  FORMAT is the
 26923    mode/header/tab line format to display.  Value is the pixel height
 26924    of the mode/header/tab line displayed.  */
 26925 
 26926 static int
 26927 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
 26928 {
 26929   struct it it;
 26930   struct face *face;
 26931   specpdl_ref count = SPECPDL_INDEX ();
 26932 
 26933   init_iterator (&it, w, -1, -1, NULL, face_id);
 26934   /* Don't extend on a previously drawn mode-line.
 26935      This may happen if called from pos_visible_p.  */
 26936   it.glyph_row->enabled_p = false;
 26937   prepare_desired_row (w, it.glyph_row, true);
 26938 
 26939   it.glyph_row->mode_line_p = true;
 26940   if (face_id == TAB_LINE_FACE_ID)
 26941     {
 26942       it.glyph_row->tab_line_p = true;
 26943       w->desired_matrix->tab_line_p = true;
 26944     }
 26945   else if (face_id == HEADER_LINE_FACE_ID)
 26946     w->desired_matrix->header_line_p = true;
 26947 
 26948   /* FIXME: This should be controlled by a user option.  But
 26949      supporting such an option is not trivial, since the mode line is
 26950      made up of many separate strings.  */
 26951   it.paragraph_embedding = L2R;
 26952 
 26953   record_unwind_protect (unwind_format_mode_line,
 26954                          format_mode_line_unwind_data (NULL, NULL,
 26955                                                        Qnil, false));
 26956 
 26957   /* Temporarily make frame's keyboard the current kboard so that
 26958      kboard-local variables in the mode_line_format will get the right
 26959      values.  */
 26960   push_kboard (FRAME_KBOARD (it.f));
 26961   record_unwind_save_match_data ();
 26962 
 26963   if (NILP (Vmode_line_compact)
 26964       || face_id == HEADER_LINE_FACE_ID || face_id == TAB_LINE_FACE_ID)
 26965     {
 26966       mode_line_target = MODE_LINE_DISPLAY;
 26967       display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 26968     }
 26969   else
 26970     {
 26971       Lisp_Object mode_string = Fformat_mode_line (format, Qnil, Qnil, Qnil);
 26972       if (EQ (Vmode_line_compact, Qlong)
 26973           && WINDOW_TOTAL_COLS (w) >= SCHARS (mode_string))
 26974         {
 26975           /* The window is wide enough; just display the mode line we
 26976              just computed. */
 26977           display_string (NULL, mode_string, Qnil,
 26978                           0, 0, &it, 0, 0, 0,
 26979                           STRING_MULTIBYTE (mode_string));
 26980         }
 26981       else
 26982         {
 26983           /* Compress the mode line. */
 26984           ptrdiff_t i = 0, i_byte = 0, start = 0;
 26985           int prev = 0;
 26986 
 26987           while (i < SCHARS (mode_string))
 26988             {
 26989               int c = fetch_string_char_advance (mode_string, &i, &i_byte);
 26990               if (c == ' ' && prev == ' ')
 26991                 {
 26992                   display_string (NULL,
 26993                                   Fsubstring (mode_string, make_fixnum (start),
 26994                                               make_fixnum (i - 1)),
 26995                                   Qnil, 0, 0, &it, 0, 0, 0,
 26996                                   STRING_MULTIBYTE (mode_string));
 26997                   /* Skip past the rest of the space characters. */
 26998                   while (c == ' ' && i < SCHARS (mode_string))
 26999                       c = fetch_string_char_advance (mode_string, &i, &i_byte);
 27000                   start = i - 1;
 27001                 }
 27002               prev = c;
 27003             }
 27004 
 27005           /* Display the final bit. */
 27006           if (start < i)
 27007             display_string (NULL,
 27008                             Fsubstring (mode_string, make_fixnum (start),
 27009                                         make_fixnum (i)),
 27010                             Qnil, 0, 0, &it, 0, 0, 0,
 27011                             STRING_MULTIBYTE (mode_string));
 27012         }
 27013     }
 27014   pop_kboard ();
 27015 
 27016   unbind_to (count, Qnil);
 27017 
 27018   /* Fill up with spaces.  */
 27019   display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
 27020 
 27021   compute_line_metrics (&it);
 27022   it.glyph_row->full_width_p = true;
 27023   it.glyph_row->continued_p = false;
 27024   it.glyph_row->truncated_on_left_p = false;
 27025   it.glyph_row->truncated_on_right_p = false;
 27026 
 27027   /* Make a 3D mode-line have a shadow at its right end.  */
 27028   face = FACE_FROM_ID (it.f, face_id);
 27029   extend_face_to_end_of_line (&it);
 27030   if (face->box != FACE_NO_BOX)
 27031     {
 27032       struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
 27033                             + it.glyph_row->used[TEXT_AREA] - 1);
 27034       int box_thickness = face->box_vertical_line_width;
 27035       last->right_box_line_p = true;
 27036       /* Add back the space for the right box line we subtracted in
 27037          init_iterator, since the right_box_line_p flag will make the
 27038          glyph wider.  We actually add only as much space as is
 27039          available for the last glyph of the modeline and whatever
 27040          space is left beyond it, since that glyph could be only
 27041          partially visible */
 27042       if (box_thickness > 0)
 27043         last->pixel_width += max (0, (box_thickness
 27044                                       - (it.current_x - it.last_visible_x)));
 27045     }
 27046 
 27047   return it.glyph_row->height;
 27048 }
 27049 
 27050 /* Move element ELT in LIST to the front of LIST.
 27051    Return the updated list.  */
 27052 
 27053 static Lisp_Object
 27054 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
 27055 {
 27056   register Lisp_Object tail, prev;
 27057   register Lisp_Object tem;
 27058 
 27059   tail = list;
 27060   prev = Qnil;
 27061   while (CONSP (tail))
 27062     {
 27063       tem = XCAR (tail);
 27064 
 27065       if (EQ (elt, tem))
 27066         {
 27067           /* Splice out the link TAIL.  */
 27068           if (NILP (prev))
 27069             list = XCDR (tail);
 27070           else
 27071             Fsetcdr (prev, XCDR (tail));
 27072 
 27073           /* Now make it the first.  */
 27074           Fsetcdr (tail, list);
 27075           return tail;
 27076         }
 27077       else
 27078         prev = tail;
 27079       tail = XCDR (tail);
 27080       maybe_quit ();
 27081     }
 27082 
 27083   /* Not found--return unchanged LIST.  */
 27084   return list;
 27085 }
 27086 
 27087 /* Subroutine to call Fset_text_properties through
 27088    internal_condition_case_n.  ARGS are the arguments of
 27089    Fset_text_properties, in order.  */
 27090 
 27091 static Lisp_Object
 27092 safe_set_text_properties (ptrdiff_t nargs, Lisp_Object *args)
 27093 {
 27094   eassert (nargs == 4);
 27095   return Fset_text_properties (args[0], args[1], args[2], args[3]);
 27096 }
 27097 
 27098 /* Contribute ELT to the mode line for window IT->w.  How it
 27099    translates into text depends on its data type.
 27100 
 27101    IT describes the display environment in which we display, as usual.
 27102 
 27103    DEPTH is the depth in recursion.  It is used to prevent
 27104    infinite recursion here.
 27105 
 27106    FIELD_WIDTH is the number of characters the display of ELT should
 27107    occupy in the mode line, and PRECISION is the maximum number of
 27108    characters to display from ELT's representation.  See
 27109    display_string for details.
 27110 
 27111    Returns the hpos of the end of the text generated by ELT.
 27112 
 27113    PROPS is a property list to add to any string we encounter.
 27114 
 27115    If RISKY, remove (disregard) any properties in any string
 27116    we encounter, and ignore :eval and :propertize.
 27117 
 27118    The global variable `mode_line_target' determines whether the
 27119    output is passed to `store_mode_line_noprop',
 27120    `store_mode_line_string', or `display_string'.  */
 27121 
 27122 static int
 27123 display_mode_element (struct it *it, int depth, int field_width, int precision,
 27124                       Lisp_Object elt, Lisp_Object props, bool risky)
 27125 {
 27126   int n = 0, field, prec;
 27127   bool literal = false;
 27128 
 27129  tail_recurse:
 27130   if (depth > 100)
 27131     elt = build_string ("*too-deep*");
 27132 
 27133   depth++;
 27134 
 27135   switch (XTYPE (elt))
 27136     {
 27137     case Lisp_String:
 27138       {
 27139         /* A string: output it and check for %-constructs within it.  */
 27140         unsigned char c;
 27141         ptrdiff_t offset = 0;
 27142 
 27143         if (SCHARS (elt) > 0
 27144             && (!NILP (props) || risky))
 27145           {
 27146             Lisp_Object oprops, aelt;
 27147             oprops = Ftext_properties_at (make_fixnum (0), elt);
 27148 
 27149             /* If the starting string's properties are not what
 27150                we want, translate the string.  Also, if the string
 27151                is risky, do that anyway.  */
 27152 
 27153             if (NILP (Fequal (props, oprops)) || risky)
 27154               {
 27155                 /* If the starting string has properties,
 27156                    merge the specified ones onto the existing ones.  */
 27157                 if (! NILP (oprops) && !risky)
 27158                   {
 27159                     Lisp_Object tem;
 27160 
 27161                     oprops = Fcopy_sequence (oprops);
 27162                     tem = props;
 27163                     while (CONSP (tem))
 27164                       {
 27165                         oprops = plist_put (oprops, XCAR (tem),
 27166                                             XCAR (XCDR (tem)));
 27167                         tem = XCDR (XCDR (tem));
 27168                       }
 27169                     props = oprops;
 27170                   }
 27171 
 27172                 aelt = Fassoc (elt, mode_line_proptrans_alist, Qnil);
 27173                 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
 27174                   {
 27175                     /* AELT is what we want.  Move it to the front
 27176                        without consing.  */
 27177                     elt = XCAR (aelt);
 27178                     mode_line_proptrans_alist
 27179                       = move_elt_to_front (aelt, mode_line_proptrans_alist);
 27180                   }
 27181                 else
 27182                   {
 27183                     Lisp_Object tem;
 27184 
 27185                     /* If AELT has the wrong props, it is useless.
 27186                        so get rid of it.  */
 27187                     if (! NILP (aelt))
 27188                       mode_line_proptrans_alist
 27189                         = Fdelq (aelt, mode_line_proptrans_alist);
 27190 
 27191                     elt = Fcopy_sequence (elt);
 27192                     /* PROPS might cause set-text-properties to signal
 27193                        an error, so we call it via internal_condition_case_n,
 27194                        to avoid an infloop in redisplay due to the error.  */
 27195                     internal_condition_case_n (safe_set_text_properties,
 27196                                                4,
 27197                                                ((Lisp_Object [])
 27198                                                {make_fixnum (0),
 27199                                                    Flength (elt),
 27200                                                    props,
 27201                                                    elt}),
 27202                                                Qt, safe_eval_handler);
 27203                     /* Add this item to mode_line_proptrans_alist.  */
 27204                     mode_line_proptrans_alist
 27205                       = Fcons (Fcons (elt, props),
 27206                                mode_line_proptrans_alist);
 27207                     /* Truncate mode_line_proptrans_alist
 27208                        to at most 50 elements.  */
 27209                     tem = Fnthcdr (make_fixnum (50),
 27210                                    mode_line_proptrans_alist);
 27211                     if (! NILP (tem))
 27212                       XSETCDR (tem, Qnil);
 27213                   }
 27214               }
 27215           }
 27216 
 27217         offset = 0;
 27218 
 27219         if (literal)
 27220           {
 27221             prec = precision - n;
 27222             switch (mode_line_target)
 27223               {
 27224               case MODE_LINE_NOPROP:
 27225               case MODE_LINE_TITLE:
 27226                 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
 27227                 break;
 27228               case MODE_LINE_STRING:
 27229                 n += store_mode_line_string (NULL, elt, true, 0, prec, Qnil);
 27230                 break;
 27231               case MODE_LINE_DISPLAY:
 27232                 n += display_string (NULL, elt, Qnil, 0, 0, it,
 27233                                      0, prec, 0, STRING_MULTIBYTE (elt));
 27234                 break;
 27235               }
 27236 
 27237             break;
 27238           }
 27239 
 27240         /* Handle the non-literal case.  */
 27241 
 27242         while ((precision <= 0 || n < precision)
 27243                && SREF (elt, offset) != 0
 27244                && (mode_line_target != MODE_LINE_DISPLAY
 27245                    || it->current_x < it->last_visible_x))
 27246           {
 27247             ptrdiff_t last_offset = offset;
 27248 
 27249             /* Advance to end of string or next format specifier.  */
 27250             while ((c = SREF (elt, offset++)) != '\0' && c != '%')
 27251               ;
 27252 
 27253             if (offset - 1 != last_offset)
 27254               {
 27255                 ptrdiff_t nchars, nbytes;
 27256 
 27257                 /* Output to end of string or up to '%'.  Field width
 27258                    is length of string.  Don't output more than
 27259                    PRECISION allows us.  */
 27260                 offset--;
 27261 
 27262                 prec = c_string_width (SDATA (elt) + last_offset,
 27263                                        offset - last_offset, precision - n,
 27264                                        &nchars, &nbytes);
 27265 
 27266                 switch (mode_line_target)
 27267                   {
 27268                   case MODE_LINE_NOPROP:
 27269                   case MODE_LINE_TITLE:
 27270                     n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
 27271                     break;
 27272                   case MODE_LINE_STRING:
 27273                     {
 27274                       ptrdiff_t bytepos = last_offset;
 27275                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27276                       ptrdiff_t endpos = (precision <= 0
 27277                                           ? string_byte_to_char (elt, offset)
 27278                                           : charpos + nchars);
 27279                       Lisp_Object mode_string
 27280                         = Fsubstring (elt, make_fixnum (charpos),
 27281                                       make_fixnum (endpos));
 27282                       n += store_mode_line_string (NULL, mode_string, false,
 27283                                                    0, 0, Qnil);
 27284                     }
 27285                     break;
 27286                   case MODE_LINE_DISPLAY:
 27287                     {
 27288                       ptrdiff_t bytepos = last_offset;
 27289                       ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
 27290 
 27291                       if (precision <= 0)
 27292                         nchars = string_byte_to_char (elt, offset) - charpos;
 27293                       n += display_string (NULL, elt, Qnil, 0, charpos,
 27294                                            it, 0, nchars, 0,
 27295                                            STRING_MULTIBYTE (elt));
 27296                     }
 27297                     break;
 27298                   }
 27299               }
 27300             else /* c == '%' */
 27301               {
 27302                 ptrdiff_t percent_position = offset;
 27303 
 27304                 /* Get the specified minimum width.  Zero means
 27305                    don't pad.  */
 27306                 field = 0;
 27307                 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
 27308                   field = field * 10 + c - '0';
 27309 
 27310                 /* Don't pad beyond the total padding allowed.  */
 27311                 if (field_width - n > 0 && field > field_width - n)
 27312                   field = field_width - n;
 27313 
 27314                 /* Note that either PRECISION <= 0 or N < PRECISION.  */
 27315                 prec = precision - n;
 27316 
 27317                 if (c == 'M')
 27318                   n += display_mode_element (it, depth, field, prec,
 27319                                              Vglobal_mode_string, props,
 27320                                              risky);
 27321                 else if (c != 0)
 27322                   {
 27323                     bool multibyte;
 27324                     ptrdiff_t bytepos, charpos;
 27325                     const char *spec;
 27326                     Lisp_Object string;
 27327 
 27328                     bytepos = percent_position;
 27329                     charpos = (STRING_MULTIBYTE (elt)
 27330                                ? string_byte_to_char (elt, bytepos)
 27331                                : bytepos);
 27332                     spec = decode_mode_spec (it->w, c, field, &string);
 27333                     eassert (NILP (string) || STRINGP (string));
 27334                     multibyte = !NILP (string) && STRING_MULTIBYTE (string);
 27335                     /* Non-ASCII characters in SPEC should cause mode-line
 27336                        element be displayed as a multibyte string.  */
 27337                     ptrdiff_t nbytes = strlen (spec);
 27338                     ptrdiff_t nchars, mb_nbytes;
 27339                     parse_str_as_multibyte ((const unsigned char *)spec, nbytes,
 27340                                             &nchars, &mb_nbytes);
 27341                     if (!(nbytes == nchars || nbytes != mb_nbytes))
 27342                       multibyte = true;
 27343 
 27344                     switch (mode_line_target)
 27345                       {
 27346                       case MODE_LINE_NOPROP:
 27347                       case MODE_LINE_TITLE:
 27348                         n += store_mode_line_noprop (spec, field, prec);
 27349                         break;
 27350                       case MODE_LINE_STRING:
 27351                         {
 27352                           Lisp_Object tem = build_string (spec);
 27353                           props = Ftext_properties_at (make_fixnum (charpos), elt);
 27354                           /* Should only keep face property in props */
 27355                           n += store_mode_line_string (NULL, tem, false,
 27356                                                        field, prec, props);
 27357                         }
 27358                         break;
 27359                       case MODE_LINE_DISPLAY:
 27360                         {
 27361                           int nglyphs_before, nwritten;
 27362 
 27363                           nglyphs_before = it->glyph_row->used[TEXT_AREA];
 27364                           nwritten = display_string (spec, string, elt,
 27365                                                      charpos, 0, it,
 27366                                                      field, prec, 0,
 27367                                                      multibyte);
 27368 
 27369                           /* Assign to the glyphs written above the
 27370                              string where the `%x' came from, position
 27371                              of the `%'.  */
 27372                           if (nwritten > 0)
 27373                             {
 27374                               struct glyph *glyph
 27375                                 = (it->glyph_row->glyphs[TEXT_AREA]
 27376                                    + nglyphs_before);
 27377                               int i;
 27378 
 27379                               for (i = 0; i < nwritten; ++i)
 27380                                 {
 27381                                   glyph[i].object = elt;
 27382                                   glyph[i].charpos = charpos;
 27383                                 }
 27384 
 27385                               n += nwritten;
 27386                             }
 27387                         }
 27388                         break;
 27389                       }
 27390                   }
 27391                 else /* c == 0 */
 27392                   break;
 27393               }
 27394           }
 27395       }
 27396       break;
 27397 
 27398     case Lisp_Symbol:
 27399       /* A symbol: process the value of the symbol recursively
 27400          as if it appeared here directly.  Avoid error if symbol void.
 27401          Special case: if value of symbol is a string, output the string
 27402          literally.  */
 27403       {
 27404         register Lisp_Object tem;
 27405 
 27406         /* If the variable is not marked as risky to set
 27407            then its contents are risky to use.  */
 27408         if (NILP (Fget (elt, Qrisky_local_variable)))
 27409           risky = true;
 27410 
 27411         tem = Fboundp (elt);
 27412         if (!NILP (tem))
 27413           {
 27414             tem = Fsymbol_value (elt);
 27415             /* If value is a string, output that string literally:
 27416                don't check for % within it.  */
 27417             if (STRINGP (tem))
 27418               literal = true;
 27419 
 27420             if (!EQ (tem, elt))
 27421               {
 27422                 /* Give up right away for nil or t.  */
 27423                 elt = tem;
 27424                 goto tail_recurse;
 27425               }
 27426           }
 27427       }
 27428       break;
 27429 
 27430     case Lisp_Cons:
 27431       {
 27432         register Lisp_Object car, tem;
 27433 
 27434         /* A cons cell: five distinct cases.
 27435            If first element is :eval or :propertize, do something special.
 27436            If first element is a string or a cons, process all the elements
 27437            and effectively concatenate them.
 27438            If first element is a negative number, truncate displaying cdr to
 27439            at most that many characters.  If positive, pad (with spaces)
 27440            to at least that many characters.
 27441            If first element is a symbol, process the cadr or caddr recursively
 27442            according to whether the symbol's value is non-nil or nil.  */
 27443         car = XCAR (elt);
 27444         if (EQ (car, QCeval))
 27445           {
 27446             /* An element of the form (:eval FORM) means evaluate FORM
 27447                and use the result as mode line elements.  */
 27448 
 27449             if (risky)
 27450               break;
 27451 
 27452             if (CONSP (XCDR (elt)))
 27453               {
 27454                 Lisp_Object spec;
 27455                 spec = safe__eval (true, XCAR (XCDR (elt)));
 27456                 /* The :eval form could delete the frame stored in the
 27457                    iterator, which will cause a crash if we try to
 27458                    access faces and other fields (e.g., FRAME_KBOARD)
 27459                    on that frame.  This is a nonsensical thing to do,
 27460                    and signaling an error from redisplay might be
 27461                    dangerous, but we cannot continue with an invalid frame.  */
 27462                 if (!FRAME_LIVE_P (it->f))
 27463                   signal_error (":eval deleted the frame being displayed", elt);
 27464                 n += display_mode_element (it, depth, field_width - n,
 27465                                            precision - n, spec, props,
 27466                                            risky);
 27467               }
 27468           }
 27469         else if (EQ (car, QCpropertize))
 27470           {
 27471             /* An element of the form (:propertize ELT PROPS...)
 27472                means display ELT but applying properties PROPS.  */
 27473 
 27474             if (risky)
 27475               break;
 27476 
 27477             if (CONSP (XCDR (elt)))
 27478               n += display_mode_element (it, depth, field_width - n,
 27479                                          precision - n, XCAR (XCDR (elt)),
 27480                                          XCDR (XCDR (elt)), risky);
 27481           }
 27482         else if (SYMBOLP (car))
 27483           {
 27484             tem = Fboundp (car);
 27485             elt = XCDR (elt);
 27486             if (!CONSP (elt))
 27487               goto invalid;
 27488             /* elt is now the cdr, and we know it is a cons cell.
 27489                Use its car if CAR has a non-nil value.  */
 27490             if (!NILP (tem))
 27491               {
 27492                 tem = Fsymbol_value (car);
 27493                 if (!NILP (tem))
 27494                   {
 27495                     elt = XCAR (elt);
 27496                     goto tail_recurse;
 27497                   }
 27498               }
 27499             /* Symbol's value is nil (or symbol is unbound)
 27500                Get the cddr of the original list
 27501                and if possible find the caddr and use that.  */
 27502             elt = XCDR (elt);
 27503             if (NILP (elt))
 27504               break;
 27505             else if (!CONSP (elt))
 27506               goto invalid;
 27507             elt = XCAR (elt);
 27508             goto tail_recurse;
 27509           }
 27510         else if (FIXNUMP (car))
 27511           {
 27512             register int lim = XFIXNUM (car);
 27513             elt = XCDR (elt);
 27514             if (lim < 0)
 27515               {
 27516                 /* Negative int means reduce maximum width.  */
 27517                 if (precision <= 0)
 27518                   precision = -lim;
 27519                 else
 27520                   precision = min (precision, -lim);
 27521               }
 27522             else if (lim > 0)
 27523               {
 27524                 /* Padding specified.  Don't let it be more than
 27525                    current maximum.  */
 27526                 if (precision > 0)
 27527                   lim = min (precision, lim);
 27528 
 27529                 /* If that's more padding than already wanted, queue it.
 27530                    But don't reduce padding already specified even if
 27531                    that is beyond the current truncation point.  */
 27532                 field_width = max (lim, field_width);
 27533               }
 27534             goto tail_recurse;
 27535           }
 27536         else if (STRINGP (car) || CONSP (car))
 27537           FOR_EACH_TAIL_SAFE (elt)
 27538             {
 27539               if (0 < precision && precision <= n)
 27540                 break;
 27541               n += display_mode_element (it, depth,
 27542                                          /* Pad after only the last
 27543                                             list element.  */
 27544                                          (! CONSP (XCDR (elt))
 27545                                           ? field_width - n
 27546                                           : 0),
 27547                                          precision - n, XCAR (elt),
 27548                                          props, risky);
 27549             }
 27550       }
 27551       break;
 27552 
 27553     default:
 27554     invalid:
 27555       elt = build_string ("*invalid*");
 27556       goto tail_recurse;
 27557     }
 27558 
 27559   /* Pad to FIELD_WIDTH.  */
 27560   if (field_width > 0 && n < field_width)
 27561     {
 27562       switch (mode_line_target)
 27563         {
 27564         case MODE_LINE_NOPROP:
 27565         case MODE_LINE_TITLE:
 27566           n += store_mode_line_noprop ("", field_width - n, 0);
 27567           break;
 27568         case MODE_LINE_STRING:
 27569           n += store_mode_line_string ("", Qnil, false, field_width - n, 0,
 27570                                        Qnil);
 27571           break;
 27572         case MODE_LINE_DISPLAY:
 27573           n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
 27574                                0, 0, 0);
 27575           break;
 27576         }
 27577     }
 27578 
 27579   return n;
 27580 }
 27581 
 27582 /* Store a mode-line string element in mode_line_string_list.
 27583 
 27584    If STRING is non-null, display that C string.  Otherwise, the Lisp
 27585    string LISP_STRING is displayed.
 27586 
 27587    FIELD_WIDTH is the minimum number of output glyphs to produce.
 27588    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 27589    with spaces.  FIELD_WIDTH <= 0 means don't pad.
 27590 
 27591    PRECISION is the maximum number of characters to output from
 27592    STRING.  PRECISION <= 0  means don't truncate the string.
 27593 
 27594    If COPY_STRING, make a copy of LISP_STRING before adding
 27595    properties to the string.
 27596 
 27597    PROPS are the properties to add to the string.
 27598    The mode_line_string_face face property is always added to the string.
 27599  */
 27600 
 27601 static int
 27602 store_mode_line_string (const char *string, Lisp_Object lisp_string,
 27603                         bool copy_string,
 27604                         int field_width, int precision, Lisp_Object props)
 27605 {
 27606   ptrdiff_t len;
 27607   int n = 0;
 27608 
 27609   if (string != NULL)
 27610     {
 27611       len = strnlen (string, precision <= 0 ? SIZE_MAX : precision);
 27612       lisp_string = make_string (string, len);
 27613       if (NILP (props))
 27614         props = mode_line_string_face_prop;
 27615       else if (!NILP (mode_line_string_face))
 27616         {
 27617           Lisp_Object face = plist_get (props, Qface);
 27618           props = Fcopy_sequence (props);
 27619           if (NILP (face))
 27620             face = mode_line_string_face;
 27621           else
 27622             face = list2 (face, mode_line_string_face);
 27623           props = plist_put (props, Qface, face);
 27624         }
 27625       Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27626                             props, lisp_string);
 27627     }
 27628   else
 27629     {
 27630       len = SCHARS (lisp_string);
 27631       if (precision > 0 && len > precision)
 27632         {
 27633           len = precision;
 27634           lisp_string = Fsubstring (lisp_string, make_fixnum (0), make_fixnum (len));
 27635           precision = -1;
 27636         }
 27637       if (!NILP (mode_line_string_face))
 27638         {
 27639           Lisp_Object face;
 27640           if (NILP (props))
 27641             props = Ftext_properties_at (make_fixnum (0), lisp_string);
 27642           face = plist_get (props, Qface);
 27643           if (NILP (face))
 27644             face = mode_line_string_face;
 27645           else
 27646             face = list2 (face, mode_line_string_face);
 27647           props = list2 (Qface, face);
 27648           if (copy_string)
 27649             lisp_string = Fcopy_sequence (lisp_string);
 27650         }
 27651       if (!NILP (props))
 27652         Fadd_text_properties (make_fixnum (0), make_fixnum (len),
 27653                               props, lisp_string);
 27654     }
 27655 
 27656   if (len > 0)
 27657     {
 27658       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27659       n += len;
 27660     }
 27661 
 27662   if (field_width > len)
 27663     {
 27664       field_width -= len;
 27665       lisp_string = Fmake_string (make_fixnum (field_width), make_fixnum (' '),
 27666                                   Qnil);
 27667       if (!NILP (props))
 27668         Fadd_text_properties (make_fixnum (0), make_fixnum (field_width),
 27669                               props, lisp_string);
 27670       mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
 27671       n += field_width;
 27672     }
 27673 
 27674   return n;
 27675 }
 27676 
 27677 
 27678 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
 27679        1, 4, 0,
 27680        doc: /* Format a string out of a mode line format specification.
 27681 First arg FORMAT specifies the mode line format (see `mode-line-format'
 27682 for details) to use.
 27683 
 27684 By default, the format is evaluated for the currently selected window.
 27685 
 27686 Optional second arg FACE specifies the face property to put on all
 27687 characters for which no face is specified.  The value nil means the
 27688 default face.  The value t means whatever face the window's mode line
 27689 currently uses (either `mode-line' or `mode-line-inactive',
 27690 depending on whether the window is the selected window or not).
 27691 An integer value means the value string has no text
 27692 properties.
 27693 
 27694 Optional third and fourth args WINDOW and BUFFER specify the window
 27695 and buffer to use as the context for the formatting (defaults
 27696 are the selected window and the WINDOW's buffer).  */)
 27697      (Lisp_Object format, Lisp_Object face,
 27698       Lisp_Object window, Lisp_Object buffer)
 27699 {
 27700   struct it it;
 27701   int len;
 27702   struct window *w;
 27703   struct buffer *old_buffer = NULL;
 27704   int face_id;
 27705   bool no_props = FIXNUMP (face);
 27706   specpdl_ref count = SPECPDL_INDEX ();
 27707   Lisp_Object str;
 27708   int string_start = 0;
 27709 
 27710   w = decode_any_window (window);
 27711   XSETWINDOW (window, w);
 27712 
 27713   if (NILP (buffer))
 27714     buffer = w->contents;
 27715   CHECK_BUFFER (buffer);
 27716 
 27717   /* Make formatting the modeline a non-op when noninteractive, otherwise
 27718      there will be problems later caused by a partially initialized frame.  */
 27719   if (NILP (format) || noninteractive)
 27720     return empty_unibyte_string;
 27721 
 27722   if (no_props)
 27723     face = Qnil;
 27724 
 27725   face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
 27726     : EQ (face, Qt) ? (EQ (window, selected_window)
 27727                        ? MODE_LINE_ACTIVE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
 27728     : EQ (face, Qmode_line_active) ? MODE_LINE_ACTIVE_FACE_ID
 27729     : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
 27730     : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
 27731     : EQ (face, Qtab_line) ? TAB_LINE_FACE_ID
 27732     : EQ (face, Qtab_bar) ? TAB_BAR_FACE_ID
 27733     : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
 27734     : DEFAULT_FACE_ID;
 27735 
 27736   old_buffer = current_buffer;
 27737 
 27738   /* Save things including mode_line_proptrans_alist,
 27739      and set that to nil so that we don't alter the outer value.  */
 27740   record_unwind_protect (unwind_format_mode_line,
 27741                          format_mode_line_unwind_data
 27742                            (XFRAME (WINDOW_FRAME (w)),
 27743                             old_buffer, selected_window, true));
 27744   mode_line_proptrans_alist = Qnil;
 27745 
 27746   Fselect_window (window, Qt);
 27747   set_buffer_internal_1 (XBUFFER (buffer));
 27748 
 27749   init_iterator (&it, w, -1, -1, NULL, face_id);
 27750 
 27751   if (no_props)
 27752     {
 27753       mode_line_target = MODE_LINE_NOPROP;
 27754       mode_line_string_face_prop = Qnil;
 27755       mode_line_string_list = Qnil;
 27756       string_start = MODE_LINE_NOPROP_LEN (0);
 27757     }
 27758   else
 27759     {
 27760       mode_line_target = MODE_LINE_STRING;
 27761       mode_line_string_list = Qnil;
 27762       mode_line_string_face = face;
 27763       mode_line_string_face_prop
 27764         = NILP (face) ? Qnil : list2 (Qface, face);
 27765     }
 27766 
 27767   push_kboard (FRAME_KBOARD (it.f));
 27768   display_mode_element (&it, 0, 0, 0, format, Qnil, false);
 27769   pop_kboard ();
 27770 
 27771   if (no_props)
 27772     {
 27773       len = MODE_LINE_NOPROP_LEN (string_start);
 27774       str = make_string (mode_line_noprop_buf + string_start, len);
 27775     }
 27776   else
 27777     {
 27778       mode_line_string_list = Fnreverse (mode_line_string_list);
 27779       str = Fmapconcat (Qidentity, mode_line_string_list,
 27780                         empty_unibyte_string);
 27781     }
 27782 
 27783   return unbind_to (count, str);
 27784 }
 27785 
 27786 /* Write a null-terminated, right justified decimal representation of
 27787    the positive integer D to BUF using a minimal field width WIDTH.  */
 27788 
 27789 static void
 27790 pint2str (register char *buf, register int width, register ptrdiff_t d)
 27791 {
 27792   register char *p = buf;
 27793 
 27794   if (d <= 0)
 27795     *p++ = '0';
 27796   else
 27797     {
 27798       while (d > 0)
 27799         {
 27800           *p++ = d % 10 + '0';
 27801           d /= 10;
 27802         }
 27803     }
 27804 
 27805   for (width -= (int) (p - buf); width > 0; --width)
 27806     *p++ = ' ';
 27807   *p-- = '\0';
 27808   while (p > buf)
 27809     {
 27810       d = *buf;
 27811       *buf++ = *p;
 27812       *p-- = d;
 27813     }
 27814 }
 27815 
 27816 /* Write a null-terminated, right justified decimal and "human
 27817    readable" representation of the nonnegative integer D to BUF using
 27818    a minimal field width WIDTH.  D should be smaller than 999.5e24. */
 27819 
 27820 static const char power_letter[] =
 27821   {
 27822     0,   /* no letter */
 27823     'k', /* kilo */
 27824     'M', /* mega */
 27825     'G', /* giga */
 27826     'T', /* tera */
 27827     'P', /* peta */
 27828     'E', /* exa */
 27829     'Z', /* zetta */
 27830     'Y', /* yotta */
 27831     'R', /* ronna */
 27832     'Q'  /* quetta */
 27833   };
 27834 
 27835 static void
 27836 pint2hrstr (char *buf, int width, ptrdiff_t d)
 27837 {
 27838   /* We aim to represent the nonnegative integer D as
 27839      QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
 27840   ptrdiff_t quotient = d;
 27841   int remainder = 0;
 27842   /* -1 means: do not use TENTHS. */
 27843   int tenths = -1;
 27844   int exponent = 0;
 27845 
 27846   /* Length of QUOTIENT.TENTHS as a string. */
 27847   int length;
 27848 
 27849   char * psuffix;
 27850   char * p;
 27851 
 27852   if (quotient >= 1000)
 27853     {
 27854       /* Scale to the appropriate EXPONENT. */
 27855       do
 27856         {
 27857           remainder = quotient % 1000;
 27858           quotient /= 1000;
 27859           exponent++;
 27860         }
 27861       while (quotient >= 1000);
 27862 
 27863       /* Round to nearest and decide whether to use TENTHS or not. */
 27864       if (quotient <= 9)
 27865         {
 27866           tenths = remainder / 100;
 27867           if (remainder % 100 >= 50)
 27868             {
 27869               if (tenths < 9)
 27870                 tenths++;
 27871               else
 27872                 {
 27873                   quotient++;
 27874                   if (quotient == 10)
 27875                     tenths = -1;
 27876                   else
 27877                     tenths = 0;
 27878                 }
 27879             }
 27880         }
 27881       else
 27882         if (remainder >= 500)
 27883           {
 27884             if (quotient < 999)
 27885               quotient++;
 27886             else
 27887               {
 27888                 quotient = 1;
 27889                 exponent++;
 27890                 tenths = 0;
 27891               }
 27892           }
 27893     }
 27894 
 27895   /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
 27896   if (tenths == -1 && quotient <= 99)
 27897     if (quotient <= 9)
 27898       length = 1;
 27899     else
 27900       length = 2;
 27901   else
 27902     length = 3;
 27903   p = psuffix = buf + max (width, length);
 27904 
 27905   /* Print EXPONENT. */
 27906   *psuffix++ = power_letter[exponent];
 27907   *psuffix = '\0';
 27908 
 27909   /* Print TENTHS. */
 27910   if (tenths >= 0)
 27911     {
 27912       *--p = '0' + tenths;
 27913       *--p = '.';
 27914     }
 27915 
 27916   /* Print QUOTIENT. */
 27917   do
 27918     {
 27919       int digit = quotient % 10;
 27920       *--p =  '0' + digit;
 27921     }
 27922   while ((quotient /= 10) != 0);
 27923 
 27924   /* Print leading spaces. */
 27925   while (buf < p)
 27926     *--p = ' ';
 27927 }
 27928 
 27929 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
 27930    If EOL_FLAG, set also a mnemonic character for end-of-line
 27931    type of CODING_SYSTEM.  Return updated pointer into BUF.  */
 27932 
 27933 static unsigned char invalid_eol_type[] = "(*invalid*)";
 27934 
 27935 static char *
 27936 decode_mode_spec_coding (Lisp_Object coding_system, char *buf, bool eol_flag)
 27937 {
 27938   Lisp_Object val;
 27939   bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
 27940   const unsigned char *eol_str;
 27941   int eol_str_len;
 27942   /* The EOL conversion we are using.  */
 27943   Lisp_Object eoltype;
 27944 
 27945   val = CODING_SYSTEM_SPEC (coding_system);
 27946   eoltype = Qnil;
 27947 
 27948   if (!VECTORP (val))           /* Not yet decided.  */
 27949     {
 27950       *buf++ = multibyte ? '-' : ' ';
 27951       if (eol_flag)
 27952         eoltype = eol_mnemonic_undecided;
 27953       /* Don't mention EOL conversion if it isn't decided.  */
 27954     }
 27955   else
 27956     {
 27957       Lisp_Object attrs;
 27958       Lisp_Object eolvalue;
 27959 
 27960       attrs = AREF (val, 0);
 27961       eolvalue = AREF (val, 2);
 27962 
 27963       if (multibyte)
 27964         buf += CHAR_STRING (XFIXNAT (CODING_ATTR_MNEMONIC (attrs)),
 27965                             (unsigned char *) buf);
 27966       else
 27967         *buf++ = ' ';
 27968 
 27969       if (eol_flag)
 27970         {
 27971           /* The EOL conversion that is normal on this system.  */
 27972 
 27973           if (NILP (eolvalue))  /* Not yet decided.  */
 27974             eoltype = eol_mnemonic_undecided;
 27975           else if (VECTORP (eolvalue)) /* Not yet decided.  */
 27976             eoltype = eol_mnemonic_undecided;
 27977           else                  /* eolvalue is Qunix, Qdos, or Qmac.  */
 27978             eoltype = (EQ (eolvalue, Qunix)
 27979                        ? eol_mnemonic_unix
 27980                        : EQ (eolvalue, Qdos)
 27981                        ? eol_mnemonic_dos : eol_mnemonic_mac);
 27982         }
 27983     }
 27984 
 27985   if (eol_flag)
 27986     {
 27987       /* Mention the EOL conversion if it is not the usual one.  */
 27988       if (STRINGP (eoltype))
 27989         {
 27990           eol_str = SDATA (eoltype);
 27991           eol_str_len = SBYTES (eoltype);
 27992         }
 27993       else if (CHARACTERP (eoltype))
 27994         {
 27995           int c = XFIXNAT (eoltype);
 27996           return buf + CHAR_STRING (c, (unsigned char *) buf);
 27997         }
 27998       else
 27999         {
 28000           eol_str = invalid_eol_type;
 28001           eol_str_len = sizeof (invalid_eol_type) - 1;
 28002         }
 28003       memcpy (buf, eol_str, eol_str_len);
 28004       buf += eol_str_len;
 28005     }
 28006 
 28007   return buf;
 28008 }
 28009 
 28010 /* Return the approximate percentage N is of D (rounding upward), or 99,
 28011    whichever is less.  Assume 0 < D and 0 <= N <= D * INT_MAX / 100.  */
 28012 
 28013 static int
 28014 percent99 (ptrdiff_t n, ptrdiff_t d)
 28015 {
 28016   int percent = (d - 1 + 100.0 * n) / d;
 28017   return min (percent, 99);
 28018 }
 28019 
 28020 /* Return a string for the output of a mode line %-spec for window W,
 28021    generated by character C.  FIELD_WIDTH > 0 means pad the string
 28022    returned with spaces to that value.  Set *STRING to be a Lisp
 28023    string if the resulting string is taken from that Lisp string;
 28024    otherwise, set *STRING to Qnil.
 28025 
 28026    Note we operate on the current buffer for most purposes.  */
 28027 
 28028 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
 28029 
 28030 static const char *
 28031 decode_mode_spec (struct window *w, register int c, int field_width,
 28032                   Lisp_Object *string)
 28033 {
 28034   Lisp_Object obj;
 28035   struct frame *f = XFRAME (WINDOW_FRAME (w));
 28036   char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
 28037   /* We are going to use f->decode_mode_spec_buffer as the buffer to
 28038      produce strings from numerical values, so limit preposterously
 28039      large values of FIELD_WIDTH to avoid overrunning the buffer's
 28040      end.  The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
 28041      bytes plus the terminating null.  */
 28042   int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
 28043   struct buffer *b = current_buffer;
 28044 
 28045   obj = Qnil;
 28046   *string = Qnil;
 28047 
 28048   switch (c)
 28049     {
 28050     case '*':
 28051       if (!NILP (BVAR (b, read_only)))
 28052         return "%";
 28053       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28054         return "*";
 28055       return "-";
 28056 
 28057     case '+':
 28058       /* This differs from %* only for a modified read-only buffer.  */
 28059       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28060         return "*";
 28061       if (!NILP (BVAR (b, read_only)))
 28062         return "%";
 28063       return "-";
 28064 
 28065     case '&':
 28066       /* This differs from %* in ignoring read-only-ness.  */
 28067       if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
 28068         return "*";
 28069       return "-";
 28070 
 28071     case '%':
 28072       return "%";
 28073 
 28074     case '[':
 28075       {
 28076         int i;
 28077         char *p;
 28078 
 28079         if (command_loop_level > 5)
 28080           return "[[[... ";
 28081         p = decode_mode_spec_buf;
 28082         for (i = 0; i < command_loop_level; i++)
 28083           *p++ = '[';
 28084         *p = 0;
 28085         return decode_mode_spec_buf;
 28086       }
 28087 
 28088     case ']':
 28089       {
 28090         int i;
 28091         char *p;
 28092 
 28093         if (command_loop_level > 5)
 28094           return " ...]]]";
 28095         p = decode_mode_spec_buf;
 28096         for (i = 0; i < command_loop_level; i++)
 28097           *p++ = ']';
 28098         *p = 0;
 28099         return decode_mode_spec_buf;
 28100       }
 28101 
 28102     case '-':
 28103       {
 28104         register int i;
 28105 
 28106         /* Let lots_of_dashes be a string of infinite length.  */
 28107         if (mode_line_target == MODE_LINE_NOPROP
 28108             || mode_line_target == MODE_LINE_STRING)
 28109           return "--";
 28110         if (field_width <= 0
 28111             || field_width > sizeof (lots_of_dashes))
 28112           {
 28113             for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
 28114               decode_mode_spec_buf[i] = '-';
 28115             decode_mode_spec_buf[i] = '\0';
 28116             return decode_mode_spec_buf;
 28117           }
 28118         else
 28119           return lots_of_dashes;
 28120       }
 28121 
 28122     case 'b':
 28123       obj = BVAR (b, name);
 28124       break;
 28125 
 28126     case 'c':
 28127     case 'C':
 28128       /* %c, %C, and %l are ignored in `frame-title-format'.
 28129          (In redisplay_internal, the frame title is drawn _before_ the
 28130          windows are updated, so the stuff which depends on actual
 28131          window contents (such as %l) may fail to render properly, or
 28132          even crash emacs.)  */
 28133       if (mode_line_target == MODE_LINE_TITLE)
 28134         return "";
 28135       else
 28136         {
 28137           ptrdiff_t col = current_column ();
 28138           int disp_col = (c == 'C') ? col + 1 : col;
 28139           w->column_number_displayed = col;
 28140           pint2str (decode_mode_spec_buf, width, disp_col);
 28141           return decode_mode_spec_buf;
 28142         }
 28143 
 28144     case 'e':
 28145 #if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
 28146       {
 28147         if (NILP (Vmemory_full))
 28148           return "";
 28149         else
 28150           return "!MEM FULL! ";
 28151       }
 28152 #else
 28153       return "";
 28154 #endif
 28155 
 28156     case 'F':
 28157       /* %F displays the frame name.  */
 28158       if (!NILP (f->title))
 28159         return SSDATA (f->title);
 28160       if (f->explicit_name || ! FRAME_WINDOW_P (f))
 28161         return SSDATA (f->name);
 28162       return "Emacs";
 28163 
 28164     case 'f':
 28165       obj = BVAR (b, filename);
 28166       break;
 28167 
 28168     case 'i':
 28169       {
 28170         ptrdiff_t size = ZV - BEGV;
 28171         pint2str (decode_mode_spec_buf, width, size);
 28172         return decode_mode_spec_buf;
 28173       }
 28174 
 28175     case 'I':
 28176       {
 28177         ptrdiff_t size = ZV - BEGV;
 28178         pint2hrstr (decode_mode_spec_buf, width, size);
 28179         return decode_mode_spec_buf;
 28180       }
 28181 
 28182     case 'l':
 28183       {
 28184         ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
 28185         ptrdiff_t topline, nlines, height;
 28186         ptrdiff_t junk;
 28187 
 28188         /* %c, %C, and %l are ignored in `frame-title-format'.  */
 28189         if (mode_line_target == MODE_LINE_TITLE)
 28190           return "";
 28191 
 28192         startpos = marker_position (w->start);
 28193         startpos_byte = marker_byte_position (w->start);
 28194         height = WINDOW_TOTAL_LINES (w);
 28195         /* We cannot cope with w->start being outside of the
 28196            accessible portion of the buffer; in particular,
 28197            display_count_lines call below might infloop if called with
 28198            startpos_byte outside of the [BEGV_BYTE..ZV_BYTE] region.
 28199            Such w->start means we were called in some "creative" way
 28200            when the buffer's restriction was changed, but the window
 28201            wasn't yet redisplayed after that.  If that happens, we
 28202            need to determine a new base line.  */
 28203         if (!(BUF_BEGV_BYTE (b) <= startpos_byte
 28204               && startpos_byte <= BUF_ZV_BYTE (b)))
 28205           {
 28206             startpos = BUF_BEGV (b);
 28207             startpos_byte = BUF_BEGV_BYTE (b);
 28208             w->base_line_pos = 0;
 28209             w->base_line_number = 0;
 28210           }
 28211 
 28212         /* If we decided that this buffer isn't suitable for line numbers,
 28213            don't forget that too fast.  */
 28214         if (w->base_line_pos == -1)
 28215           goto no_value;
 28216 
 28217         /* If the buffer is very big, don't waste time.  */
 28218         if (FIXNUMP (Vline_number_display_limit)
 28219             && BUF_ZV (b) - BUF_BEGV (b) > XFIXNUM (Vline_number_display_limit))
 28220           {
 28221             w->base_line_pos = 0;
 28222             w->base_line_number = 0;
 28223             goto no_value;
 28224           }
 28225 
 28226         if (w->base_line_number > 0
 28227             && w->base_line_pos > 0
 28228             && w->base_line_pos <= startpos)
 28229           {
 28230             line = w->base_line_number;
 28231             linepos = w->base_line_pos;
 28232             linepos_byte = buf_charpos_to_bytepos (b, linepos);
 28233           }
 28234         else
 28235           {
 28236             line = 1;
 28237             linepos = BUF_BEGV (b);
 28238             linepos_byte = BUF_BEGV_BYTE (b);
 28239           }
 28240 
 28241         /* Count lines from base line to window start position.  */
 28242         nlines = display_count_lines (linepos_byte,
 28243                                       startpos_byte,
 28244                                       startpos, &junk);
 28245 
 28246         topline = nlines + line;
 28247 
 28248         /* Determine a new base line, if the old one is too close
 28249            or too far away, or if we did not have one.
 28250            "Too close" means it's plausible a scroll-down would
 28251            go back past it.  */
 28252         if (startpos == BUF_BEGV (b))
 28253           {
 28254             w->base_line_number = topline;
 28255             w->base_line_pos = BUF_BEGV (b);
 28256           }
 28257         else if (nlines < height + 25 || nlines > height * 3 + 50
 28258                  || linepos == BUF_BEGV (b))
 28259           {
 28260             ptrdiff_t limit = BUF_BEGV (b);
 28261             ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
 28262             ptrdiff_t position;
 28263             ptrdiff_t distance
 28264               = (line_number_display_limit_width < 0 ? 0
 28265                  : ckd_mul (&distance, line_number_display_limit_width,
 28266                             height * 2 + 30)
 28267                  ? PTRDIFF_MAX : distance);
 28268 
 28269             if (startpos - distance > limit)
 28270               {
 28271                 limit = startpos - distance;
 28272                 limit_byte = CHAR_TO_BYTE (limit);
 28273               }
 28274 
 28275             nlines = display_count_lines (startpos_byte,
 28276                                           limit_byte,
 28277                                           - (height * 2 + 30),
 28278                                           &position);
 28279             /* If we couldn't find the lines we wanted within
 28280                line_number_display_limit_width chars per line,
 28281                give up on line numbers for this window.  */
 28282             if (position == limit_byte && limit == startpos - distance)
 28283               {
 28284                 w->base_line_pos = -1;
 28285                 w->base_line_number = 0;
 28286                 goto no_value;
 28287               }
 28288 
 28289             w->base_line_number = topline - nlines;
 28290             w->base_line_pos = BYTE_TO_CHAR (position);
 28291           }
 28292 
 28293         /* Now count lines from the start pos to point.  */
 28294         nlines = display_count_lines (startpos_byte,
 28295                                       PT_BYTE, PT, &junk);
 28296 
 28297         /* Record that we did display the line number.  */
 28298         line_number_displayed = true;
 28299 
 28300         /* Make the string to show.  */
 28301         pint2str (decode_mode_spec_buf, width, topline + nlines);
 28302         return decode_mode_spec_buf;
 28303     no_value:
 28304         {
 28305           char *p = decode_mode_spec_buf;
 28306           int pad = width - 2;
 28307           while (pad-- > 0)
 28308             *p++ = ' ';
 28309           *p++ = '?';
 28310           *p++ = '?';
 28311           *p = '\0';
 28312           return decode_mode_spec_buf;
 28313         }
 28314       }
 28315       break;
 28316 
 28317     case 'm':
 28318       obj = BVAR (b, mode_name);
 28319       break;
 28320 
 28321     case 'n':
 28322       if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
 28323         return " Narrow";
 28324       break;
 28325 
 28326       /* Display the "degree of travel" of the window through the buffer.  */
 28327     case 'o':
 28328       {
 28329         ptrdiff_t toppos = marker_position (w->start);
 28330         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28331         ptrdiff_t begv = BUF_BEGV (b);
 28332         ptrdiff_t zv = BUF_ZV (b);
 28333 
 28334         if (zv <= botpos)
 28335           return toppos <= begv ? "All" : "Bottom";
 28336         else if (toppos <= begv)
 28337           return "Top";
 28338         else
 28339           {
 28340           sprintf (decode_mode_spec_buf, "%2d%%",
 28341                    percent99 (toppos - begv, (toppos - begv) + (zv - botpos)));
 28342           return decode_mode_spec_buf;
 28343           }
 28344       }
 28345 
 28346       /* Display percentage of buffer above the top of the screen.  */
 28347     case 'p':
 28348       {
 28349         ptrdiff_t pos = marker_position (w->start);
 28350         ptrdiff_t begv = BUF_BEGV (b);
 28351         ptrdiff_t zv = BUF_ZV (b);
 28352 
 28353         if (w->window_end_pos <= BUF_Z (b) - zv)
 28354           return pos <= begv ? "All" : "Bottom";
 28355         else if (pos <= begv)
 28356           return "Top";
 28357         else
 28358           {
 28359             sprintf (decode_mode_spec_buf, "%2d%%",
 28360                      percent99 (pos - begv, zv - begv));
 28361             return decode_mode_spec_buf;
 28362           }
 28363       }
 28364 
 28365       /* Display percentage of size above the bottom of the screen.  */
 28366     case 'P':
 28367       {
 28368         ptrdiff_t toppos = marker_position (w->start);
 28369         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28370         ptrdiff_t begv = BUF_BEGV (b);
 28371         ptrdiff_t zv = BUF_ZV (b);
 28372 
 28373         if (zv <= botpos)
 28374           return toppos <= begv ? "All" : "Bottom";
 28375         else
 28376           {
 28377             sprintf (decode_mode_spec_buf,
 28378                      &"Top%2d%%"[begv < toppos ? sizeof "Top" - 1 : 0],
 28379                      percent99 (botpos - begv, zv - begv));
 28380             return decode_mode_spec_buf;
 28381           }
 28382       }
 28383 
 28384       /* Display percentage offsets of top and bottom of the window,
 28385          using "All" (but not "Top" or "Bottom") where appropriate.  */
 28386     case 'q':
 28387       {
 28388         ptrdiff_t toppos = marker_position (w->start);
 28389         ptrdiff_t botpos = BUF_Z (b) - w->window_end_pos;
 28390         ptrdiff_t begv = BUF_BEGV (b);
 28391         ptrdiff_t zv = BUF_ZV (b);
 28392         int top_perc, bot_perc;
 28393 
 28394         if ((toppos <= begv) && (zv <= botpos))
 28395           return "All   ";
 28396 
 28397         top_perc = toppos <= begv ? 0 : percent99 (toppos - begv, zv - begv);
 28398         bot_perc = zv <= botpos ? 100 : percent99 (botpos - begv, zv - begv);
 28399 
 28400         if (top_perc == bot_perc)
 28401           sprintf (decode_mode_spec_buf, "%d%%", top_perc);
 28402         else
 28403           sprintf (decode_mode_spec_buf, "%d-%d%%", top_perc, bot_perc);
 28404 
 28405         return decode_mode_spec_buf;
 28406       }
 28407 
 28408     case 's':
 28409       /* status of process */
 28410       obj = Fget_buffer_process (Fcurrent_buffer ());
 28411       if (NILP (obj))
 28412         return "no process";
 28413 #ifndef MSDOS
 28414       obj = Fsymbol_name (Fprocess_status (obj));
 28415 #endif
 28416       break;
 28417 
 28418     case '@':
 28419       {
 28420         specpdl_ref count = inhibit_garbage_collection ();
 28421         Lisp_Object curdir = BVAR (current_buffer, directory);
 28422         Lisp_Object val = Qnil;
 28423 
 28424         if (STRINGP (curdir))
 28425           val = safe_call1 (intern ("file-remote-p"), curdir);
 28426 
 28427         val = unbind_to (count, val);
 28428 
 28429         if (NILP (val))
 28430           return "-";
 28431         else
 28432           return "@";
 28433       }
 28434 
 28435     case 'z':
 28436       /* coding-system (not including end-of-line format) */
 28437     case 'Z':
 28438       /* coding-system (including end-of-line type) */
 28439       {
 28440         bool eol_flag = (c == 'Z');
 28441         char *p = decode_mode_spec_buf;
 28442 
 28443         if (! FRAME_WINDOW_P (f))
 28444           {
 28445             /* No need to mention EOL here--the terminal never needs
 28446                to do EOL conversion.  */
 28447             p = decode_mode_spec_coding (CODING_ID_NAME
 28448                                          (FRAME_KEYBOARD_CODING (f)->id),
 28449                                          p, false);
 28450             p = decode_mode_spec_coding (CODING_ID_NAME
 28451                                          (FRAME_TERMINAL_CODING (f)->id),
 28452                                          p, false);
 28453           }
 28454         p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
 28455                                      p, eol_flag);
 28456 
 28457 #if false /* This proves to be annoying; I think we can do without. -- rms.  */
 28458 #ifdef subprocesses
 28459         obj = Fget_buffer_process (Fcurrent_buffer ());
 28460         if (PROCESSP (obj))
 28461           {
 28462             p = decode_mode_spec_coding
 28463               (XPROCESS (obj)->decode_coding_system, p, eol_flag);
 28464             p = decode_mode_spec_coding
 28465               (XPROCESS (obj)->encode_coding_system, p, eol_flag);
 28466           }
 28467 #endif /* subprocesses */
 28468 #endif /* false */
 28469         *p = 0;
 28470         return decode_mode_spec_buf;
 28471       }
 28472     }
 28473 
 28474   if (STRINGP (obj))
 28475     {
 28476       *string = obj;
 28477       return SSDATA (obj);
 28478     }
 28479   else
 28480     return "";
 28481 }
 28482 
 28483 /* Return the number of lines between start_byte and end_byte in the
 28484    current buffer. */
 28485 
 28486 ptrdiff_t
 28487 count_lines (ptrdiff_t start_byte, ptrdiff_t end_byte)
 28488 {
 28489   ptrdiff_t ignored;
 28490   return display_count_lines (start_byte, end_byte, ZV, &ignored);
 28491 }
 28492 
 28493 /* Count up to COUNT lines starting from START_BYTE.  COUNT negative
 28494    means count lines back from START_BYTE.  But don't go beyond
 28495    LIMIT_BYTE.  Return the number of lines thus found (always
 28496    nonnegative).
 28497 
 28498    Set *BYTE_POS_PTR to the byte position where we stopped.  This is
 28499    either the position COUNT lines after/before START_BYTE, if we
 28500    found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
 28501    COUNT lines.  */
 28502 
 28503 static ptrdiff_t
 28504 display_count_lines (ptrdiff_t start_byte,
 28505                      ptrdiff_t limit_byte, ptrdiff_t count,
 28506                      ptrdiff_t *byte_pos_ptr)
 28507 {
 28508   register unsigned char *cursor;
 28509   unsigned char *base;
 28510 
 28511   register ptrdiff_t ceiling;
 28512   register unsigned char *ceiling_addr;
 28513   ptrdiff_t orig_count = count;
 28514 
 28515   /* If we are not in selective display mode,
 28516      check only for newlines.  */
 28517   bool selective_display
 28518     = (!NILP (BVAR (current_buffer, selective_display))
 28519        && !FIXNUMP (BVAR (current_buffer, selective_display)));
 28520 
 28521   if (count > 0)
 28522     {
 28523       while (start_byte < limit_byte)
 28524         {
 28525           ceiling =  BUFFER_CEILING_OF (start_byte);
 28526           ceiling = min (limit_byte - 1, ceiling);
 28527           ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
 28528           base = (cursor = BYTE_POS_ADDR (start_byte));
 28529 
 28530           do
 28531             {
 28532               if (selective_display)
 28533                 {
 28534                   while (*cursor != '\n' && *cursor != 015
 28535                          && ++cursor != ceiling_addr)
 28536                     continue;
 28537                   if (cursor == ceiling_addr)
 28538                     break;
 28539                 }
 28540               else
 28541                 {
 28542                   cursor = memchr (cursor, '\n', ceiling_addr - cursor);
 28543                   if (! cursor)
 28544                     break;
 28545                 }
 28546 
 28547               cursor++;
 28548 
 28549               if (--count == 0)
 28550                 {
 28551                   start_byte += cursor - base;
 28552                   *byte_pos_ptr = start_byte;
 28553                   return orig_count;
 28554                 }
 28555             }
 28556           while (cursor < ceiling_addr);
 28557 
 28558           start_byte += ceiling_addr - base;
 28559         }
 28560     }
 28561   else
 28562     {
 28563       while (start_byte > limit_byte)
 28564         {
 28565           ceiling = BUFFER_FLOOR_OF (start_byte - 1);
 28566           ceiling = max (limit_byte, ceiling);
 28567           ceiling_addr = BYTE_POS_ADDR (ceiling);
 28568           base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
 28569           while (true)
 28570             {
 28571               if (selective_display)
 28572                 {
 28573                   while (--cursor >= ceiling_addr
 28574                          && *cursor != '\n' && *cursor != 015)
 28575                     continue;
 28576                   if (cursor < ceiling_addr)
 28577                     break;
 28578                 }
 28579               else
 28580                 {
 28581                   cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
 28582                   if (! cursor)
 28583                     break;
 28584                 }
 28585 
 28586               if (++count == 0)
 28587                 {
 28588                   start_byte += cursor - base + 1;
 28589                   *byte_pos_ptr = start_byte;
 28590                   /* When scanning backwards, we should
 28591                      not count the newline posterior to which we stop.  */
 28592                   return - orig_count - 1;
 28593                 }
 28594             }
 28595           start_byte += ceiling_addr - base;
 28596         }
 28597     }
 28598 
 28599   *byte_pos_ptr = limit_byte;
 28600 
 28601   if (count < 0)
 28602     return - orig_count + count;
 28603   return orig_count - count;
 28604 
 28605 }
 28606 
 28607 
 28608 
 28609 /***********************************************************************
 28610                          Displaying strings
 28611  ***********************************************************************/
 28612 
 28613 /* Display a NUL-terminated string, starting with index START.
 28614 
 28615    If STRING is non-null, display that C string.  Otherwise, the Lisp
 28616    string LISP_STRING is displayed.  There's a case that STRING is
 28617    non-null and LISP_STRING is not nil.  It means STRING is a string
 28618    data of LISP_STRING.  In that case, we display LISP_STRING while
 28619    ignoring its text properties.
 28620 
 28621    If FACE_STRING is not nil, FACE_STRING_POS is a position in
 28622    FACE_STRING.  Display STRING or LISP_STRING with the face at
 28623    FACE_STRING_POS in FACE_STRING:
 28624 
 28625    Display the string in the environment given by IT, but use the
 28626    standard display table, temporarily.
 28627 
 28628    FIELD_WIDTH is the minimum number of output glyphs to produce.
 28629    If STRING has fewer characters than FIELD_WIDTH, pad to the right
 28630    with spaces.  If STRING has more characters, more than FIELD_WIDTH
 28631    glyphs will be produced.  FIELD_WIDTH <= 0 means don't pad.
 28632 
 28633    PRECISION is the maximum number of characters to output from
 28634    STRING.  PRECISION < 0  means don't truncate the string.
 28635 
 28636    This is roughly equivalent to printf format specifiers:
 28637 
 28638    FIELD_WIDTH  PRECISION       PRINTF
 28639    ----------------------------------------
 28640    -1           -1              %s
 28641    -1           10              %.10s
 28642    10           -1              %10s
 28643    20           10              %20.10s
 28644 
 28645    MULTIBYTE zero means do not display multibyte chars, > 0 means do
 28646    display them, and < 0 means obey the current buffer's value of
 28647    enable_multibyte_characters.
 28648 
 28649    Value is the number of columns displayed.  */
 28650 
 28651 static int
 28652 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
 28653                 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
 28654                 int field_width, int precision, int max_x, int multibyte)
 28655 {
 28656   int hpos_at_start = it->hpos;
 28657   int saved_face_id = it->face_id;
 28658   struct glyph_row *row = it->glyph_row;
 28659   ptrdiff_t it_charpos;
 28660 
 28661   /* Initialize the iterator IT for iteration over STRING beginning
 28662      with index START.  */
 28663   reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string,
 28664                     start, precision, field_width, multibyte);
 28665 
 28666   if (string && STRINGP (lisp_string))
 28667     /* LISP_STRING is the one returned by decode_mode_spec.  We should
 28668        ignore its text properties.  */
 28669     it->stop_charpos = it->end_charpos;
 28670 
 28671   /* If displaying STRING, set up the face of the iterator from
 28672      FACE_STRING, if that's given.  */
 28673   if (STRINGP (face_string))
 28674     {
 28675       ptrdiff_t endptr;
 28676       struct face *face;
 28677 
 28678       it->face_id
 28679         = face_at_string_position (it->w, face_string, face_string_pos,
 28680                                    0, &endptr, it->base_face_id, false, 0);
 28681       face = FACE_FROM_ID (it->f, it->face_id);
 28682       it->face_box_p = face->box != FACE_NO_BOX;
 28683 
 28684       /* If we have a display spec, but there's no Lisp string being
 28685          displayed, then check whether we've got one from the
 28686          :propertize being passed in and use that.  */
 28687       if (NILP (lisp_string))
 28688         {
 28689           Lisp_Object display = Fget_text_property (make_fixnum (0), Qdisplay,
 28690                                                     face_string);
 28691           if (!NILP (display))
 28692             {
 28693               Lisp_Object min_width = plist_get (display, Qmin_width);
 28694               if (!NILP (min_width))
 28695                 display_min_width (it, 0, face_string, min_width);
 28696             }
 28697         }
 28698     }
 28699 
 28700   /* Set max_x to the maximum allowed X position.  Don't let it go
 28701      beyond the right edge of the window.  */
 28702   if (max_x <= 0)
 28703     max_x = it->last_visible_x;
 28704   else
 28705     max_x = min (max_x, it->last_visible_x);
 28706 
 28707   /* Skip over display elements that are not visible because IT->w is
 28708      hscrolled.  */
 28709   if (it->current_x < it->first_visible_x)
 28710     move_it_in_display_line_to (it, 100000, it->first_visible_x,
 28711                                 MOVE_TO_POS | MOVE_TO_X);
 28712 
 28713   row->ascent = it->max_ascent;
 28714   row->height = it->max_ascent + it->max_descent;
 28715   row->phys_ascent = it->max_phys_ascent;
 28716   row->phys_height = it->max_phys_ascent + it->max_phys_descent;
 28717   row->extra_line_spacing = it->max_extra_line_spacing;
 28718 
 28719   if (STRINGP (it->string))
 28720     it_charpos = IT_STRING_CHARPOS (*it);
 28721   else
 28722     it_charpos = IT_CHARPOS (*it);
 28723 
 28724   /* This condition is for the case that we are called with current_x
 28725      past last_visible_x.  */
 28726   while (it->current_x < max_x)
 28727     {
 28728       int x_before, x, n_glyphs_before, i, nglyphs;
 28729 
 28730       /* Get the next display element.  */
 28731       if (!get_next_display_element (it))
 28732         break;
 28733 
 28734       /* Produce glyphs.  */
 28735       x_before = it->current_x;
 28736       n_glyphs_before = row->used[TEXT_AREA];
 28737       PRODUCE_GLYPHS (it);
 28738 
 28739       nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
 28740       i = 0;
 28741       x = x_before;
 28742       while (i < nglyphs)
 28743         {
 28744           struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
 28745 
 28746           if (it->line_wrap != TRUNCATE
 28747               && x + glyph->pixel_width > max_x)
 28748             {
 28749               /* End of continued line or max_x reached.  */
 28750               if (CHAR_GLYPH_PADDING_P (*glyph))
 28751                 {
 28752                   /* A wide character is unbreakable.  */
 28753                   if (row->reversed_p)
 28754                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28755                                       - n_glyphs_before);
 28756                   row->used[TEXT_AREA] = n_glyphs_before;
 28757                   it->current_x = x_before;
 28758                 }
 28759               else
 28760                 {
 28761                   if (row->reversed_p)
 28762                     unproduce_glyphs (it, row->used[TEXT_AREA]
 28763                                       - (n_glyphs_before + i));
 28764                   row->used[TEXT_AREA] = n_glyphs_before + i;
 28765                   it->current_x = x;
 28766                 }
 28767               break;
 28768             }
 28769           else if (x + glyph->pixel_width >= it->first_visible_x)
 28770             {
 28771               /* Glyph is at least partially visible.  */
 28772               ++it->hpos;
 28773               if (x < it->first_visible_x)
 28774                 row->x = x - it->first_visible_x;
 28775             }
 28776           else
 28777             {
 28778               /* Glyph is off the left margin of the display area.
 28779                  Should not happen.  */
 28780               emacs_abort ();
 28781             }
 28782 
 28783           row->ascent = max (row->ascent, it->max_ascent);
 28784           row->height = max (row->height, it->max_ascent + it->max_descent);
 28785           row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
 28786           row->phys_height = max (row->phys_height,
 28787                                   it->max_phys_ascent + it->max_phys_descent);
 28788           row->extra_line_spacing = max (row->extra_line_spacing,
 28789                                          it->max_extra_line_spacing);
 28790           x += glyph->pixel_width;
 28791           ++i;
 28792         }
 28793 
 28794       /* Stop if max_x reached.  */
 28795       if (i < nglyphs)
 28796         break;
 28797 
 28798       /* Stop at line ends.  */
 28799       if (ITERATOR_AT_END_OF_LINE_P (it))
 28800         {
 28801           it->continuation_lines_width = 0;
 28802           break;
 28803         }
 28804 
 28805       set_iterator_to_next (it, true);
 28806       if (STRINGP (it->string))
 28807         it_charpos = IT_STRING_CHARPOS (*it);
 28808       else
 28809         it_charpos = IT_CHARPOS (*it);
 28810 
 28811       /* Stop if truncating at the right edge.  */
 28812       if (it->line_wrap == TRUNCATE
 28813           && it->current_x >= it->last_visible_x)
 28814         {
 28815           /* Add truncation mark, but don't do it if the line is
 28816              truncated at a padding space.  */
 28817           /* Need to do the below for the last string character as
 28818              well, since it could be a double-width character, in
 28819              which case the previous character ends before
 28820              last_visible_x.  Thus, comparison with <=, not <.  */
 28821           if (it_charpos <= it->string_nchars)
 28822             {
 28823               if (!FRAME_WINDOW_P (it->f))
 28824                 {
 28825                   int ii, n;
 28826 
 28827                   if (it->current_x > it->last_visible_x)
 28828                     {
 28829                       /* This flag is true if we are displaying mode
 28830                          line, false for header-line or tab-line.  */
 28831                       bool mode_line_p = false;
 28832 
 28833                       /* ROW->mode_line_p is true if we display mode
 28834                          line or header-line or tab-line.  */
 28835                       if (row->mode_line_p)
 28836                         {
 28837                           struct window *w = it->w;
 28838                           if (row == MATRIX_MODE_LINE_ROW (w->desired_matrix))
 28839                             mode_line_p = true;
 28840                         }
 28841                       if (!row->reversed_p)
 28842                         {
 28843                           for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
 28844                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28845                               break;
 28846                         }
 28847                       else
 28848                         {
 28849                           for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
 28850                             if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
 28851                               break;
 28852                           unproduce_glyphs (it, ii + 1);
 28853                           ii = row->used[TEXT_AREA] - (ii + 1);
 28854                         }
 28855                       for (n = row->used[TEXT_AREA]; ii < n; ++ii)
 28856                         {
 28857                           row->used[TEXT_AREA] = ii;
 28858                           if (row->mode_line_p)
 28859                             pad_mode_line (it, mode_line_p);
 28860                           else
 28861                             produce_special_glyphs (it, IT_TRUNCATION);
 28862                         }
 28863                     }
 28864                   produce_special_glyphs (it, IT_TRUNCATION);
 28865                 }
 28866               row->truncated_on_right_p = true;
 28867             }
 28868           break;
 28869         }
 28870     }
 28871 
 28872   /* Maybe insert a truncation at the left.  */
 28873   if (it->first_visible_x
 28874       && it_charpos > 0)
 28875     {
 28876       if (!FRAME_WINDOW_P (it->f)
 28877           || (row->reversed_p
 28878               ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 28879               : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
 28880         insert_left_trunc_glyphs (it);
 28881       row->truncated_on_left_p = true;
 28882     }
 28883 
 28884   it->face_id = saved_face_id;
 28885 
 28886   /* Value is number of columns displayed.  */
 28887   return it->hpos - hpos_at_start;
 28888 }
 28889 
 28890 
 28891 
 28892 /* This is like a combination of memq and assq.  Return 1/2 if PROPVAL
 28893    appears as an element of LIST or as the car of an element of LIST.
 28894    If PROPVAL is a list, compare each element against LIST in that
 28895    way, and return 1/2 if any element of PROPVAL is found in LIST.
 28896    Otherwise return 0.  This function cannot quit.
 28897    The return value is 2 if the text is invisible but with an ellipsis
 28898    and 1 if it's invisible and without an ellipsis.  */
 28899 
 28900 int
 28901 invisible_prop (Lisp_Object propval, Lisp_Object list)
 28902 {
 28903   Lisp_Object tail, proptail;
 28904 
 28905   for (tail = list; CONSP (tail); tail = XCDR (tail))
 28906     {
 28907       register Lisp_Object tem;
 28908       tem = XCAR (tail);
 28909       if (EQ (propval, tem))
 28910         return 1;
 28911       if (CONSP (tem) && EQ (propval, XCAR (tem)))
 28912         return NILP (XCDR (tem)) ? 1 : 2;
 28913     }
 28914 
 28915   if (CONSP (propval))
 28916     {
 28917       for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
 28918         {
 28919           Lisp_Object propelt;
 28920           propelt = XCAR (proptail);
 28921           for (tail = list; CONSP (tail); tail = XCDR (tail))
 28922             {
 28923               register Lisp_Object tem;
 28924               tem = XCAR (tail);
 28925               if (EQ (propelt, tem))
 28926                 return 1;
 28927               if (CONSP (tem) && EQ (propelt, XCAR (tem)))
 28928                 return NILP (XCDR (tem)) ? 1 : 2;
 28929             }
 28930         }
 28931     }
 28932 
 28933   return 0;
 28934 }
 28935 
 28936 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
 28937        doc: /* Non-nil if text properties at POS cause text there to be currently invisible.
 28938 POS should be a marker or a buffer position; the value of the `invisible'
 28939 property at that position in the current buffer is examined.
 28940 POS can also be the actual value of the `invisible' text or overlay
 28941 property of the text of interest, in which case the value itself is
 28942 examined.
 28943 
 28944 The non-nil value returned can be t for currently invisible text that is
 28945 entirely hidden on display, or some other non-nil, non-t value if the
 28946 text is replaced by an ellipsis.
 28947 
 28948 Note that whether text with `invisible' property is actually hidden on
 28949 display may depend on `buffer-invisibility-spec', which see.  */)
 28950   (Lisp_Object pos)
 28951 {
 28952   Lisp_Object prop
 28953     = (FIXNATP (pos) || MARKERP (pos)
 28954        ? Fget_char_property (pos, Qinvisible, Qnil)
 28955        : pos);
 28956   int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
 28957   return (invis == 0 ? Qnil
 28958           : invis == 1 ? Qt
 28959           : make_fixnum (invis));
 28960 }
 28961 
 28962 /* Calculate a width or height in pixels from a specification using
 28963    the following elements:
 28964 
 28965    SPEC ::=
 28966      NUM      - a (fractional) multiple of the default font width/height
 28967      (NUM)    - specifies exactly NUM pixels
 28968      UNIT     - a fixed number of pixels, see below.
 28969      ELEMENT  - size of a display element in pixels, see below.
 28970      (NUM . SPEC) - equals NUM * SPEC
 28971      (+ SPEC SPEC ...)  - add pixel values
 28972      (- SPEC SPEC ...)  - subtract pixel values
 28973      (- SPEC)           - negate pixel value
 28974 
 28975    NUM ::=
 28976      INT or FLOAT   - a number constant
 28977      SYMBOL         - use symbol's (buffer local) variable binding.
 28978 
 28979    UNIT ::=
 28980      in       - pixels per inch  *)
 28981      mm       - pixels per 1/1000 meter  *)
 28982      cm       - pixels per 1/100 meter   *)
 28983      width    - width of current font in pixels.
 28984      height   - height of current font in pixels.
 28985 
 28986      *) using the ratio(s) defined in display-pixels-per-inch.
 28987 
 28988    ELEMENT ::=
 28989 
 28990      left-fringe          - left fringe width in pixels
 28991      right-fringe         - right fringe width in pixels
 28992 
 28993      left-margin          - left margin width in pixels
 28994      right-margin         - right margin width in pixels
 28995 
 28996      scroll-bar           - scroll-bar area width in pixels
 28997 
 28998    Examples:
 28999 
 29000    Pixels corresponding to 5 inches:
 29001      (5 . in)
 29002 
 29003    Total width of non-text areas on left side of window (if scroll-bar is on left):
 29004      '(space :width (+ left-fringe left-margin scroll-bar))
 29005 
 29006    Align to first text column (in header line):
 29007      '(space :align-to 0)
 29008 
 29009    Align to middle of text area minus half the width of variable `my-image'
 29010    containing a loaded image:
 29011      '(space :align-to (0.5 . (- text my-image)))
 29012 
 29013    Width of left margin minus width of 1 character in the default font:
 29014      '(space :width (- left-margin 1))
 29015 
 29016    Width of left margin minus width of 2 characters in the current font:
 29017      '(space :width (- left-margin (2 . width)))
 29018 
 29019    Center 1 character over left-margin (in header line):
 29020      '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
 29021 
 29022    Different ways to express width of left fringe plus left margin minus one pixel:
 29023      '(space :width (- (+ left-fringe left-margin) (1)))
 29024      '(space :width (+ left-fringe left-margin (- (1))))
 29025      '(space :width (+ left-fringe left-margin (-1)))
 29026 
 29027    If ALIGN_TO is NULL, returns the result in *RES.  If ALIGN_TO is
 29028    non-NULL, the value of *ALIGN_TO is a window-relative pixel
 29029    coordinate, and *RES is the additional pixel width from that point
 29030    till the end of the stretch glyph.
 29031 
 29032    WIDTH_P non-zero means take the width dimension or X coordinate of
 29033    the object specified by PROP, WIDTH_P zero means take the height
 29034    dimension or the Y coordinate.  (Therefore, if ALIGN_TO is
 29035    non-NULL, WIDTH_P should be non-zero.)
 29036 
 29037    FONT is the font of the face of the surrounding text.
 29038 
 29039    The return value is non-zero if width or height were successfully
 29040    calculated, i.e. if PROP is a valid spec.  */
 29041 
 29042 static bool
 29043 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 29044                             struct font *font, bool width_p, int *align_to)
 29045 {
 29046   /* Don't adjust for line number if we didn't yet produce it for this
 29047      screen line.  This is for when this function is called from
 29048      move_it_in_display_line_to that was called by display_line to get
 29049      past the glyphs hscrolled off the left side of the window.  */
 29050   int lnum_pixel_width = it->line_number_produced_p ? it->lnum_pixel_width : 0;
 29051   double pixels;
 29052 
 29053 # define OK_PIXELS(val) (*res = (val), true)
 29054 # define OK_ALIGN_TO(val) (*align_to = (val), true)
 29055 
 29056   if (NILP (prop))
 29057     return OK_PIXELS (0);
 29058 
 29059   eassert (FRAME_LIVE_P (it->f));
 29060 
 29061   if (SYMBOLP (prop))
 29062     {
 29063       if (SCHARS (SYMBOL_NAME (prop)) == 2)
 29064         {
 29065           char *unit = SSDATA (SYMBOL_NAME (prop));
 29066 
 29067           /* The UNIT expression, e.g. as part of (NUM . UNIT).  */
 29068           if (unit[0] == 'i' && unit[1] == 'n')
 29069             pixels = 1.0;
 29070           else if (unit[0] == 'm' && unit[1] == 'm')
 29071             pixels = 25.4;
 29072           else if (unit[0] == 'c' && unit[1] == 'm')
 29073             pixels = 2.54;
 29074           else
 29075             pixels = 0;
 29076           if (pixels > 0)
 29077             {
 29078               double ppi = (width_p ? FRAME_RES_X (it->f)
 29079                             : FRAME_RES_Y (it->f));
 29080 
 29081               if (ppi > 0)
 29082                 return OK_PIXELS (ppi / pixels);
 29083               return false;
 29084             }
 29085         }
 29086 
 29087 #ifdef HAVE_WINDOW_SYSTEM
 29088       /* 'height': the height of FONT.  */
 29089       if (EQ (prop, Qheight))
 29090         return OK_PIXELS (font
 29091                           ? normal_char_height (font, -1)
 29092                           : FRAME_LINE_HEIGHT (it->f));
 29093       /* 'width': the width of FONT.  */
 29094       if (EQ (prop, Qwidth))
 29095         return OK_PIXELS (font
 29096                           ? FONT_WIDTH (font)
 29097                           : FRAME_COLUMN_WIDTH (it->f));
 29098 #else
 29099       if (EQ (prop, Qheight) || EQ (prop, Qwidth))
 29100         return OK_PIXELS (1);
 29101 #endif
 29102 
 29103       /* 'text': the width or height of the text area.  */
 29104       if (EQ (prop, Qtext))
 29105           return OK_PIXELS (width_p
 29106                             ? (window_box_width (it->w, TEXT_AREA)
 29107                                - lnum_pixel_width)
 29108                             : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
 29109 
 29110       /* ':align_to'.  First time we compute the value, window
 29111          elements are interpreted as the position of the element's
 29112          left edge.  */
 29113       if (align_to && *align_to < 0)
 29114         {
 29115           *res = 0;
 29116           /* 'left': left edge of the text area.  */
 29117           if (EQ (prop, Qleft))
 29118             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29119                                 + lnum_pixel_width);
 29120           /* 'right': right edge of the text area.  */
 29121           if (EQ (prop, Qright))
 29122             return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
 29123           /* 'center': the center of the text area.  */
 29124           if (EQ (prop, Qcenter))
 29125             return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
 29126                                 + lnum_pixel_width
 29127                                 + window_box_width (it->w, TEXT_AREA) / 2);
 29128           /* 'left-fringe': left edge of the left fringe.  */
 29129           if (EQ (prop, Qleft_fringe))
 29130             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29131                                 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
 29132                                 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
 29133           /* 'right-fringe': left edge of the right fringe.  */
 29134           if (EQ (prop, Qright_fringe))
 29135             return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29136                                 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29137                                 : window_box_right_offset (it->w, TEXT_AREA));
 29138           /* 'left-margin': left edge of the left display margin.  */
 29139           if (EQ (prop, Qleft_margin))
 29140             return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
 29141           /* 'right-margin': left edge of the right display margin.  */
 29142           if (EQ (prop, Qright_margin))
 29143             return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
 29144           /* 'scroll-bar': left edge of the vertical scroll bar.  */
 29145           if (EQ (prop, Qscroll_bar))
 29146             return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
 29147                                 ? 0
 29148                                 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
 29149                                    + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
 29150                                       ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
 29151                                       : 0)));
 29152         }
 29153       else
 29154         {
 29155           /* Otherwise, the elements stand for their width.  */
 29156           if (EQ (prop, Qleft_fringe))
 29157             return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
 29158           if (EQ (prop, Qright_fringe))
 29159             return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
 29160           if (EQ (prop, Qleft_margin))
 29161             return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
 29162           if (EQ (prop, Qright_margin))
 29163             return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
 29164           if (EQ (prop, Qscroll_bar))
 29165             return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
 29166         }
 29167 
 29168       prop = buffer_local_value (prop, it->w->contents);
 29169       if (BASE_EQ (prop, Qunbound))
 29170         prop = Qnil;
 29171     }
 29172 
 29173   if (NUMBERP (prop))
 29174     {
 29175       int base_unit = (width_p
 29176                        ? FRAME_COLUMN_WIDTH (it->f)
 29177                        : FRAME_LINE_HEIGHT (it->f));
 29178       if (width_p && align_to && *align_to < 0)
 29179         return OK_PIXELS (XFLOATINT (prop) * base_unit + lnum_pixel_width);
 29180       return OK_PIXELS (XFLOATINT (prop) * base_unit);
 29181     }
 29182 
 29183   if (CONSP (prop))
 29184     {
 29185       Lisp_Object car = XCAR (prop);
 29186       Lisp_Object cdr = XCDR (prop);
 29187 
 29188       if (SYMBOLP (car))
 29189         {
 29190 #ifdef HAVE_WINDOW_SYSTEM
 29191           /* '(image PROPS...)': width or height of the specified image.  */
 29192           if (FRAME_WINDOW_P (it->f)
 29193               && valid_image_p (prop))
 29194             {
 29195               ptrdiff_t id = lookup_image (it->f, prop, it->face_id);
 29196               struct image *img = IMAGE_FROM_ID (it->f, id);
 29197 
 29198               return OK_PIXELS (width_p ? img->width : img->height);
 29199             }
 29200           /* '(xwidget PROPS...)': dimensions of the specified xwidget.  */
 29201           if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
 29202             {
 29203               /* TODO: Don't return dummy size.  */
 29204               return OK_PIXELS (100);
 29205             }
 29206 #endif
 29207           /* '(+ EXPR...)' or '(- EXPR...)' add or subtract
 29208              recursively calculated values.  */
 29209           if (EQ (car, Qplus) || EQ (car, Qminus))
 29210             {
 29211               bool first = true;
 29212               double px;
 29213 
 29214               pixels = 0;
 29215               while (CONSP (cdr))
 29216                 {
 29217                   if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
 29218                                                    font, width_p, align_to))
 29219                     return false;
 29220                   if (first)
 29221                     pixels = (EQ (car, Qplus) ? px : -px), first = false;
 29222                   else
 29223                     pixels += px;
 29224                   cdr = XCDR (cdr);
 29225                 }
 29226               if (EQ (car, Qminus))
 29227                 pixels = -pixels;
 29228               return OK_PIXELS (pixels);
 29229             }
 29230 
 29231           car = buffer_local_value (car, it->w->contents);
 29232           if (BASE_EQ (car, Qunbound))
 29233             car = Qnil;
 29234         }
 29235 
 29236       /* '(NUM)': absolute number of pixels.  */
 29237       if (NUMBERP (car))
 29238         {
 29239           double fact;
 29240           int offset =
 29241             width_p && align_to && *align_to < 0 ? lnum_pixel_width : 0;
 29242           pixels = XFLOATINT (car);
 29243           if (NILP (cdr))
 29244             return OK_PIXELS (pixels + offset);
 29245           if (calc_pixel_width_or_height (&fact, it, cdr,
 29246                                           font, width_p, align_to))
 29247             return OK_PIXELS (pixels * fact + offset);
 29248           return false;
 29249         }
 29250 
 29251       return false;
 29252     }
 29253 
 29254   return false;
 29255 }
 29256 
 29257 void
 29258 get_font_ascent_descent (struct font *font, int *ascent, int *descent)
 29259 {
 29260 #ifdef HAVE_WINDOW_SYSTEM
 29261   normal_char_ascent_descent (font, -1, ascent, descent);
 29262 #else
 29263   *ascent = 1;
 29264   *descent = 0;
 29265 #endif
 29266 }
 29267 
 29268 
 29269 /***********************************************************************
 29270                              Glyph Display
 29271  ***********************************************************************/
 29272 
 29273 #ifdef HAVE_WINDOW_SYSTEM
 29274 
 29275 #ifdef GLYPH_DEBUG
 29276 
 29277 extern void dump_glyph_string (struct glyph_string *) EXTERNALLY_VISIBLE;
 29278 void
 29279 dump_glyph_string (struct glyph_string *s)
 29280 {
 29281   fputs ("glyph string\n", stderr);
 29282   fprintf (stderr, "  x, y, w, h = %d, %d, %d, %d\n",
 29283            s->x, s->y, s->width, s->height);
 29284   fprintf (stderr, "  ybase = %d\n", s->ybase);
 29285   fprintf (stderr, "  hl = %u\n", s->hl);
 29286   fprintf (stderr, "  left overhang = %d, right = %d\n",
 29287            s->left_overhang, s->right_overhang);
 29288   fprintf (stderr, "  nchars = %d\n", s->nchars);
 29289   fprintf (stderr, "  extends to end of line = %d\n",
 29290            s->extends_to_end_of_line_p);
 29291   fprintf (stderr, "  font height = %d\n", FONT_HEIGHT (s->font));
 29292   fprintf (stderr, "  bg width = %d\n", s->background_width);
 29293 }
 29294 
 29295 #endif /* GLYPH_DEBUG */
 29296 
 29297 /* Initialize glyph string S.  CHAR2B is a suitably allocated vector
 29298    of 2-byte unsigned integers for S; it can't be allocated in
 29299    init_glyph_string because it must be allocated via `alloca'.  W
 29300    is the window on which S is drawn.  ROW and AREA are the glyph row
 29301    and area within the row from which S is constructed.  START is the
 29302    index of the first glyph structure covered by S.  HL is a
 29303    face-override for drawing S.  */
 29304 
 29305 #ifdef HAVE_NTGUI
 29306 /* We set inhibit-quit here due to paranoia: get_frame_dc acquires the
 29307    critical section, and we cannot QUIT while we hold the critical
 29308    section.  If any of the code run by callers of ALLOCATE_HDC happens
 29309    to call Lisp (might be possible due to all the hooks lying around),
 29310    we must prevent it from quitting.  */
 29311 # define ALLOCATE_HDC(hdc, f)                   \
 29312   Lisp_Object prev_quit = Vinhibit_quit;        \
 29313   Vinhibit_quit = Qt;                           \
 29314   HDC hdc = get_frame_dc ((f))
 29315 # define RELEASE_HDC(hdc, f)                    \
 29316   release_frame_dc ((f), (hdc));                \
 29317   Vinhibit_quit = prev_quit
 29318 #else
 29319 # define ALLOCATE_HDC(hdc, f)
 29320 # define RELEASE_HDC(hdc, f)
 29321 #endif
 29322 
 29323 static void
 29324 init_glyph_string (struct glyph_string *s,
 29325 #ifdef HAVE_NTGUI
 29326                    HDC hdc,
 29327 #endif
 29328                    unsigned *char2b, struct window *w, struct glyph_row *row,
 29329                    enum glyph_row_area area, int start, enum draw_glyphs_face hl)
 29330 {
 29331   memset (s, 0, sizeof *s);
 29332   s->w = w;
 29333   s->f = XFRAME (w->frame);
 29334 #ifdef HAVE_NTGUI
 29335   s->hdc = hdc;
 29336 #endif
 29337   s->char2b = char2b;
 29338   s->hl = hl;
 29339   s->row = row;
 29340   s->area = area;
 29341   s->first_glyph = row->glyphs[area] + start;
 29342   s->height = row->height;
 29343   s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
 29344   s->ybase = s->y + row->ascent;
 29345 }
 29346 
 29347 
 29348 /* Append the list of glyph strings with head H and tail T to the list
 29349    with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the result.  */
 29350 
 29351 static void
 29352 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29353                            struct glyph_string *h, struct glyph_string *t)
 29354 {
 29355   if (h)
 29356     {
 29357       if (*head)
 29358         (*tail)->next = h;
 29359       else
 29360         *head = h;
 29361       h->prev = *tail;
 29362       *tail = t;
 29363     }
 29364 }
 29365 
 29366 
 29367 /* Prepend the list of glyph strings with head H and tail T to the
 29368    list with head *HEAD and tail *TAIL.  Set *HEAD and *TAIL to the
 29369    result.  */
 29370 
 29371 static void
 29372 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
 29373                             struct glyph_string *h, struct glyph_string *t)
 29374 {
 29375   if (h)
 29376     {
 29377       if (*head)
 29378         (*head)->prev = t;
 29379       else
 29380         *tail = t;
 29381       t->next = *head;
 29382       *head = h;
 29383     }
 29384 }
 29385 
 29386 
 29387 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
 29388    Set *HEAD and *TAIL to the resulting list.  */
 29389 
 29390 static void
 29391 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
 29392                      struct glyph_string *s)
 29393 {
 29394   s->next = s->prev = NULL;
 29395   append_glyph_string_lists (head, tail, s, s);
 29396 }
 29397 
 29398 
 29399 /* Get face and two-byte form of character C in face FACE_ID on frame F.
 29400    The encoding of C is returned in *CHAR2B.  DISPLAY_P means
 29401    make sure that X resources for the face returned are allocated.
 29402    Value is a pointer to a realized face that is ready for display if
 29403    DISPLAY_P.  */
 29404 
 29405 static struct face *
 29406 get_char_face_and_encoding (struct frame *f, int c, int face_id,
 29407                             unsigned *char2b, bool display_p)
 29408 {
 29409   struct face *face = FACE_FROM_ID (f, face_id);
 29410   unsigned code = 0;
 29411 
 29412   if (face->font)
 29413     {
 29414       code = face->font->driver->encode_char (face->font, c);
 29415 
 29416       if (code == FONT_INVALID_CODE)
 29417         code = 0;
 29418     }
 29419   /* Ensure that the code is only 2 bytes wide.  */
 29420   *char2b = code & 0xFFFF;
 29421 
 29422   /* Make sure X resources of the face are allocated.  */
 29423 #ifdef HAVE_X_WINDOWS
 29424   if (display_p)
 29425 #endif
 29426     {
 29427       eassert (face != NULL);
 29428       prepare_face_for_display (f, face);
 29429     }
 29430 
 29431   return face;
 29432 }
 29433 
 29434 
 29435 /* Get face and two-byte form of character glyph GLYPH on frame F.
 29436    The encoding of GLYPH->u.ch is returned in *CHAR2B.  Value is
 29437    a pointer to a realized face that is ready for display.  */
 29438 
 29439 static struct face *
 29440 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
 29441                              unsigned *char2b)
 29442 {
 29443   struct face *face;
 29444   unsigned code = 0;
 29445 
 29446   eassert (glyph->type == CHAR_GLYPH);
 29447   face = FACE_FROM_ID (f, glyph->face_id);
 29448 
 29449   /* Make sure X resources of the face are allocated.  */
 29450   prepare_face_for_display (f, face);
 29451 
 29452   if (face->font)
 29453     {
 29454       if (CHAR_BYTE8_P (glyph->u.ch))
 29455         code = CHAR_TO_BYTE8 (glyph->u.ch);
 29456       else
 29457         code = face->font->driver->encode_char (face->font, glyph->u.ch);
 29458 
 29459       if (code == FONT_INVALID_CODE)
 29460         code = 0;
 29461     }
 29462 
 29463   /* Ensure that the code is only 2 bytes wide.  */
 29464   *char2b = code & 0xFFFF;
 29465   return face;
 29466 }
 29467 
 29468 
 29469 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
 29470    Return true iff FONT has a glyph for C.  */
 29471 
 29472 static bool
 29473 get_char_glyph_code (int c, struct font *font, unsigned *char2b)
 29474 {
 29475   unsigned code;
 29476 
 29477   if (CHAR_BYTE8_P (c))
 29478     code = CHAR_TO_BYTE8 (c);
 29479   else
 29480     code = font->driver->encode_char (font, c);
 29481 
 29482   if (code == FONT_INVALID_CODE)
 29483     return false;
 29484 
 29485   /* Ensure that the code is only 2 bytes wide.  */
 29486   *char2b = code & 0xFFFF;
 29487   return true;
 29488 }
 29489 
 29490 
 29491 /* Fill glyph string S with composition components specified by S->cmp.
 29492 
 29493    BASE_FACE is the base face of the composition.
 29494    S->cmp_from is the index of the first component for S.
 29495 
 29496    OVERLAPS non-zero means S should draw the foreground only, and use
 29497    its physical height for clipping.  See also draw_glyphs.
 29498 
 29499    Value is the index of a component not in S.  */
 29500 
 29501 static int
 29502 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
 29503                              int overlaps)
 29504 {
 29505   int i;
 29506   /* For all glyphs of this composition, starting at the offset
 29507      S->cmp_from, until we reach the end of the definition or encounter a
 29508      glyph that requires the different face, add it to S.  */
 29509   struct face *face;
 29510 
 29511   eassert (s);
 29512 
 29513   s->for_overlaps = overlaps;
 29514   s->face = NULL;
 29515   s->font = NULL;
 29516   for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
 29517     {
 29518       int c = COMPOSITION_GLYPH (s->cmp, i);
 29519 
 29520       /* TAB in a composition means display glyphs with padding space
 29521          on the left or right.  */
 29522       if (c != '\t')
 29523         {
 29524           int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
 29525                                        -1, Qnil);
 29526 
 29527           face = get_char_face_and_encoding (s->f, c, face_id,
 29528                                              s->char2b + i, true);
 29529           if (face)
 29530             {
 29531               if (! s->face)
 29532                 {
 29533                   s->face = face;
 29534                   s->font = s->face->font;
 29535                 }
 29536               else if (s->face != face)
 29537                 break;
 29538             }
 29539         }
 29540       ++s->nchars;
 29541     }
 29542   s->cmp_to = i;
 29543 
 29544   if (s->face == NULL)
 29545     {
 29546       s->face = base_face->ascii_face;
 29547       s->font = s->face->font;
 29548     }
 29549 
 29550   if (s->hl == DRAW_MOUSE_FACE
 29551       || (s->hl == DRAW_CURSOR
 29552           && MATRIX_ROW (s->w->current_matrix,
 29553                          s->w->phys_cursor.vpos)->mouse_face_p
 29554           && cursor_in_mouse_face_p (s->w)))
 29555     {
 29556       int c = COMPOSITION_GLYPH (s->cmp, 0);
 29557       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29558       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29559       if (!s->face)
 29560         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29561 
 29562       s->face = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face, c, -1, Qnil));
 29563       prepare_face_for_display (s->f, s->face);
 29564     }
 29565 
 29566   /* All glyph strings for the same composition has the same width,
 29567      i.e. the width set for the first component of the composition.  */
 29568   s->width = s->first_glyph->pixel_width;
 29569 
 29570   /* If the specified font could not be loaded, use the frame's
 29571      default font, but record the fact that we couldn't load it in
 29572      the glyph string so that we can draw rectangles for the
 29573      characters of the glyph string.  */
 29574   if (s->font == NULL)
 29575     {
 29576       s->font_not_found_p = true;
 29577       s->font = FRAME_FONT (s->f);
 29578     }
 29579 
 29580   /* Adjust base line for subscript/superscript text.  */
 29581   s->ybase += s->first_glyph->voffset;
 29582 
 29583   return s->cmp_to;
 29584 }
 29585 
 29586 static int
 29587 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
 29588                            int start, int end, int overlaps)
 29589 {
 29590   struct glyph *glyph, *last;
 29591   int voffset;
 29592   Lisp_Object lgstring;
 29593   int i;
 29594   bool glyph_not_available_p;
 29595 
 29596   s->for_overlaps = overlaps;
 29597   glyph = s->row->glyphs[s->area] + start;
 29598   last = s->row->glyphs[s->area] + end;
 29599   voffset = glyph->voffset;
 29600   glyph_not_available_p = glyph->glyph_not_available_p;
 29601   s->cmp_id = glyph->u.cmp.id;
 29602   s->cmp_from = glyph->slice.cmp.from;
 29603   s->cmp_to = glyph->slice.cmp.to + 1;
 29604   if (s->hl == DRAW_MOUSE_FACE
 29605       || (s->hl == DRAW_CURSOR
 29606           && MATRIX_ROW (s->w->current_matrix,
 29607                          s->w->phys_cursor.vpos)->mouse_face_p
 29608           && cursor_in_mouse_face_p (s->w)))
 29609     {
 29610       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29611       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29612       if (!s->face)
 29613         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29614       prepare_face_for_display (s->f, s->face);
 29615     }
 29616   else
 29617     s->face = FACE_FROM_ID (s->f, face_id);
 29618   lgstring = composition_gstring_from_id (s->cmp_id);
 29619   s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
 29620   /* The width of a composition glyph string is the sum of the
 29621      composition's glyph widths.  */
 29622   s->width = s->first_glyph->pixel_width;
 29623   glyph++;
 29624   while (glyph < last
 29625          && glyph->u.cmp.automatic
 29626          && glyph->u.cmp.id == s->cmp_id
 29627          && glyph->face_id == face_id
 29628          && s->cmp_to == glyph->slice.cmp.from
 29629          && glyph->glyph_not_available_p == glyph_not_available_p)
 29630     {
 29631       s->width += glyph->pixel_width;
 29632       s->cmp_to = (glyph++)->slice.cmp.to + 1;
 29633     }
 29634 
 29635   for (i = s->cmp_from; i < s->cmp_to; i++)
 29636     {
 29637       Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
 29638       unsigned code = LGLYPH_CODE (lglyph);
 29639 
 29640       /* Ensure that the code is only 2 bytes wide.  */
 29641       s->char2b[i] = code & 0xFFFF;
 29642     }
 29643 
 29644   /* If the specified font could not be loaded, record that fact in
 29645      S->font_not_found_p so that we can draw rectangles for the
 29646      characters of the glyph string.  */
 29647   if (glyph_not_available_p)
 29648     s->font_not_found_p = true;
 29649 
 29650   /* Adjust base line for subscript/superscript text.  */
 29651   s->ybase += voffset;
 29652 
 29653   return glyph - s->row->glyphs[s->area];
 29654 }
 29655 
 29656 
 29657 /* Fill glyph string S from a sequence glyphs for glyphless characters.
 29658    See the comment of fill_glyph_string for arguments.
 29659    Value is the index of the first glyph not in S.  */
 29660 
 29661 
 29662 static int
 29663 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
 29664                              int start, int end, int overlaps)
 29665 {
 29666   struct glyph *glyph, *last;
 29667   int voffset;
 29668 
 29669   eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
 29670   s->for_overlaps = overlaps;
 29671   glyph = s->row->glyphs[s->area] + start;
 29672   last = s->row->glyphs[s->area] + end;
 29673   voffset = glyph->voffset;
 29674   s->face = FACE_FROM_ID (s->f, face_id);
 29675   s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
 29676   if (s->hl == DRAW_MOUSE_FACE
 29677       || (s->hl == DRAW_CURSOR
 29678           && MATRIX_ROW (s->w->current_matrix,
 29679                          s->w->phys_cursor.vpos)->mouse_face_p
 29680           && cursor_in_mouse_face_p (s->w)))
 29681     {
 29682       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29683       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29684       if (!s->face)
 29685         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29686       prepare_face_for_display (s->f, s->face);
 29687     }
 29688   s->nchars = 1;
 29689   s->width = glyph->pixel_width;
 29690   glyph++;
 29691   while (glyph < last
 29692          && glyph->type == GLYPHLESS_GLYPH
 29693          && glyph->voffset == voffset
 29694          && glyph->face_id == face_id)
 29695     {
 29696       s->nchars++;
 29697       s->width += glyph->pixel_width;
 29698       glyph++;
 29699     }
 29700   s->ybase += voffset;
 29701   return glyph - s->row->glyphs[s->area];
 29702 }
 29703 
 29704 
 29705 /* Fill glyph string S from a sequence of character glyphs.
 29706 
 29707    FACE_ID is the face id of the string.  START is the index of the
 29708    first glyph to consider, END is the index of the last + 1.
 29709    OVERLAPS non-zero means S should draw the foreground only, and use
 29710    its physical height for clipping.  See also draw_glyphs.
 29711 
 29712    Value is the index of the first glyph not in S.  */
 29713 
 29714 static int
 29715 fill_glyph_string (struct glyph_string *s, int face_id,
 29716                    int start, int end, int overlaps)
 29717 {
 29718   struct glyph *glyph, *last;
 29719   int voffset;
 29720   bool glyph_not_available_p;
 29721 
 29722   eassert (s->f == XFRAME (s->w->frame));
 29723   eassert (s->nchars == 0);
 29724   eassert (start >= 0 && end > start);
 29725 
 29726   s->for_overlaps = overlaps;
 29727   glyph = s->row->glyphs[s->area] + start;
 29728   last = s->row->glyphs[s->area] + end;
 29729   voffset = glyph->voffset;
 29730   s->padding_p = glyph->padding_p;
 29731   glyph_not_available_p = glyph->glyph_not_available_p;
 29732 
 29733   while (glyph < last
 29734          && glyph->type == CHAR_GLYPH
 29735          && glyph->voffset == voffset
 29736          /* Same face id implies same font, nowadays.  */
 29737          && glyph->face_id == face_id
 29738          && glyph->glyph_not_available_p == glyph_not_available_p)
 29739     {
 29740       s->face = get_glyph_face_and_encoding (s->f, glyph,
 29741                                              s->char2b + s->nchars);
 29742       ++s->nchars;
 29743       eassert (s->nchars <= end - start);
 29744       s->width += glyph->pixel_width;
 29745       if (glyph++->padding_p != s->padding_p)
 29746         break;
 29747     }
 29748 
 29749   s->font = s->face->font;
 29750 
 29751   if (s->hl == DRAW_MOUSE_FACE
 29752       || (s->hl == DRAW_CURSOR
 29753           && MATRIX_ROW (s->w->current_matrix,
 29754                          s->w->phys_cursor.vpos)->mouse_face_p
 29755           && cursor_in_mouse_face_p (s->w)))
 29756     {
 29757       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29758       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29759       if (!s->face)
 29760         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29761       s->face
 29762         = FACE_FROM_ID (s->f, FACE_FOR_CHAR (s->f, s->face,
 29763                                              s->first_glyph->u.ch, -1, Qnil));
 29764       prepare_face_for_display (s->f, s->face);
 29765     }
 29766 
 29767   /* If the specified font could not be loaded, use the frame's font,
 29768      but record the fact that we couldn't load it in
 29769      S->font_not_found_p so that we can draw rectangles for the
 29770      characters of the glyph string.  */
 29771   if (s->font == NULL || glyph_not_available_p)
 29772     {
 29773       s->font_not_found_p = true;
 29774       s->font = FRAME_FONT (s->f);
 29775     }
 29776 
 29777   /* Adjust base line for subscript/superscript text.  */
 29778   s->ybase += voffset;
 29779 
 29780   eassert (s->face && s->face->gc);
 29781   return glyph - s->row->glyphs[s->area];
 29782 }
 29783 
 29784 
 29785 /* Fill glyph string S from image glyph S->first_glyph.  */
 29786 
 29787 static void
 29788 fill_image_glyph_string (struct glyph_string *s)
 29789 {
 29790   eassert (s->first_glyph->type == IMAGE_GLYPH);
 29791   s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
 29792   eassert (s->img);
 29793   s->slice = s->first_glyph->slice.img;
 29794   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29795   s->font = s->face->font;
 29796   if (s->hl == DRAW_MOUSE_FACE
 29797       || (s->hl == DRAW_CURSOR
 29798           && MATRIX_ROW (s->w->current_matrix,
 29799                          s->w->phys_cursor.vpos)->mouse_face_p
 29800           && cursor_in_mouse_face_p (s->w)))
 29801     {
 29802       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29803       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29804       if (!s->face)
 29805         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29806       prepare_face_for_display (s->f, s->face);
 29807     }
 29808   s->width = s->first_glyph->pixel_width;
 29809 
 29810   /* Adjust base line for subscript/superscript text.  */
 29811   s->ybase += s->first_glyph->voffset;
 29812 }
 29813 
 29814 
 29815 #ifdef HAVE_XWIDGETS
 29816 static void
 29817 fill_xwidget_glyph_string (struct glyph_string *s)
 29818 {
 29819   eassert (s->first_glyph->type == XWIDGET_GLYPH);
 29820   s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
 29821   s->font = s->face->font;
 29822   if (s->hl == DRAW_MOUSE_FACE
 29823       || (s->hl == DRAW_CURSOR
 29824           && MATRIX_ROW (s->w->current_matrix,
 29825                          s->w->phys_cursor.vpos)->mouse_face_p
 29826           && cursor_in_mouse_face_p (s->w)))
 29827     {
 29828       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29829       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29830       if (!s->face)
 29831         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29832       prepare_face_for_display (s->f, s->face);
 29833     }
 29834   s->width = s->first_glyph->pixel_width;
 29835   s->ybase += s->first_glyph->voffset;
 29836   s->xwidget = xwidget_from_id (s->first_glyph->u.xwidget);
 29837 }
 29838 #endif
 29839 /* Fill glyph string S from a sequence of stretch glyphs.
 29840 
 29841    START is the index of the first glyph to consider,
 29842    END is the index of the last + 1.
 29843 
 29844    Value is the index of the first glyph not in S.  */
 29845 
 29846 static int
 29847 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
 29848 {
 29849   struct glyph *glyph, *last;
 29850   int voffset, face_id;
 29851 
 29852   eassert (s->first_glyph->type == STRETCH_GLYPH);
 29853 
 29854   glyph = s->row->glyphs[s->area] + start;
 29855   last = s->row->glyphs[s->area] + end;
 29856   face_id = glyph->face_id;
 29857   s->face = FACE_FROM_ID (s->f, face_id);
 29858   s->font = s->face->font;
 29859   if (s->hl == DRAW_MOUSE_FACE
 29860       || (s->hl == DRAW_CURSOR
 29861           && MATRIX_ROW (s->w->current_matrix,
 29862                          s->w->phys_cursor.vpos)->mouse_face_p
 29863           && cursor_in_mouse_face_p (s->w)))
 29864     {
 29865       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (s->f);
 29866       s->face = FACE_FROM_ID_OR_NULL (s->f, hlinfo->mouse_face_face_id);
 29867       if (!s->face)
 29868         s->face = FACE_FROM_ID (s->f, MOUSE_FACE_ID);
 29869       prepare_face_for_display (s->f, s->face);
 29870     }
 29871   s->width = glyph->pixel_width;
 29872   s->nchars = 1;
 29873   voffset = glyph->voffset;
 29874 
 29875   for (++glyph;
 29876        (glyph < last
 29877         && glyph->type == STRETCH_GLYPH
 29878         && glyph->voffset == voffset
 29879         && glyph->face_id == face_id);
 29880        ++glyph)
 29881     s->width += glyph->pixel_width;
 29882 
 29883   /* Adjust base line for subscript/superscript text.  */
 29884   s->ybase += voffset;
 29885 
 29886   /* The case that face->gc == 0 is handled when drawing the glyph
 29887      string by calling prepare_face_for_display.  */
 29888   eassert (s->face);
 29889   return glyph - s->row->glyphs[s->area];
 29890 }
 29891 
 29892 static struct font_metrics *
 29893 get_per_char_metric (struct font *font, const unsigned *char2b)
 29894 {
 29895   static struct font_metrics metrics;
 29896 
 29897   if (! font)
 29898     return NULL;
 29899   if (*char2b == FONT_INVALID_CODE)
 29900     return NULL;
 29901 
 29902   font->driver->text_extents (font, char2b, 1, &metrics);
 29903   return &metrics;
 29904 }
 29905 
 29906 /* A subroutine that computes "normal" values of ASCENT and DESCENT
 29907    for FONT.  Values are taken from font-global ones, except for fonts
 29908    that claim preposterously large values, but whose glyphs actually
 29909    have reasonable dimensions.  C is the character to use for metrics
 29910    if the font-global values are too large; if C is negative, the
 29911    function selects a default character.  */
 29912 static void
 29913 normal_char_ascent_descent (struct font *font, int c, int *ascent, int *descent)
 29914 {
 29915   *ascent = FONT_BASE (font);
 29916   *descent = FONT_DESCENT (font);
 29917 
 29918   if (FONT_TOO_HIGH (font))
 29919     {
 29920       unsigned char2b;
 29921 
 29922       /* Get metrics of C, defaulting to a reasonably sized ASCII
 29923          character.  */
 29924       if (get_char_glyph_code (c >= 0 ? c : '{', font, &char2b))
 29925         {
 29926           struct font_metrics *pcm = get_per_char_metric (font, &char2b);
 29927           eassume (pcm);
 29928 
 29929           if (!(pcm->width == 0 && pcm->rbearing == 0 && pcm->lbearing == 0))
 29930             {
 29931               /* We add 1 pixel to character dimensions as heuristics
 29932                  that produces nicer display, e.g. when the face has
 29933                  the box attribute.  */
 29934               *ascent = pcm->ascent + 1;
 29935               *descent = pcm->descent + 1;
 29936             }
 29937         }
 29938     }
 29939 }
 29940 
 29941 /* A subroutine that computes a reasonable "normal character height"
 29942    for fonts that claim preposterously large vertical dimensions, but
 29943    whose glyphs are actually reasonably sized.  C is the character
 29944    whose metrics to use for those fonts, or -1 for default
 29945    character.  */
 29946 static int
 29947 normal_char_height (struct font *font, int c)
 29948 {
 29949   int ascent, descent;
 29950 
 29951   normal_char_ascent_descent (font, c, &ascent, &descent);
 29952 
 29953   return ascent + descent;
 29954 }
 29955 
 29956 /* EXPORT for RIF:
 29957    Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
 29958    frame F.  Overhangs of glyphs other than type CHAR_GLYPH are
 29959    assumed to be zero.  */
 29960 
 29961 void
 29962 gui_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
 29963 {
 29964   *left = *right = 0;
 29965 
 29966   if (glyph->type == CHAR_GLYPH)
 29967     {
 29968       unsigned char2b;
 29969       struct face *face = get_glyph_face_and_encoding (f, glyph, &char2b);
 29970       if (face->font)
 29971         {
 29972           struct font_metrics *pcm = get_per_char_metric (face->font, &char2b);
 29973           if (pcm)
 29974             {
 29975               if (pcm->rbearing > pcm->width)
 29976                 *right = pcm->rbearing - pcm->width;
 29977               if (pcm->lbearing < 0)
 29978                 *left = -pcm->lbearing;
 29979             }
 29980         }
 29981     }
 29982   else if (glyph->type == COMPOSITE_GLYPH)
 29983     {
 29984       if (! glyph->u.cmp.automatic)
 29985         {
 29986           struct composition *cmp = composition_table[glyph->u.cmp.id];
 29987 
 29988           if (cmp->rbearing > cmp->pixel_width)
 29989             *right = cmp->rbearing - cmp->pixel_width;
 29990           if (cmp->lbearing < 0)
 29991             *left = - cmp->lbearing;
 29992         }
 29993       else
 29994         {
 29995           Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
 29996           struct font_metrics metrics;
 29997 
 29998           composition_gstring_width (gstring, glyph->slice.cmp.from,
 29999                                      glyph->slice.cmp.to + 1, &metrics);
 30000           if (metrics.rbearing > metrics.width)
 30001             *right = metrics.rbearing - metrics.width;
 30002           if (metrics.lbearing < 0)
 30003             *left = - metrics.lbearing;
 30004         }
 30005     }
 30006 }
 30007 
 30008 
 30009 /* Return the index of the first glyph preceding glyph string S that
 30010    is overwritten by S because of S's left overhang.  Value is -1
 30011    if no glyphs are overwritten.  */
 30012 
 30013 static int
 30014 left_overwritten (struct glyph_string *s)
 30015 {
 30016   int k;
 30017 
 30018   if (s->left_overhang)
 30019     {
 30020       int x = 0, i;
 30021       struct glyph *glyphs = s->row->glyphs[s->area];
 30022       int first = s->first_glyph - glyphs;
 30023 
 30024       for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
 30025         x -= glyphs[i].pixel_width;
 30026 
 30027       k = i + 1;
 30028     }
 30029   else
 30030     k = -1;
 30031 
 30032   return k;
 30033 }
 30034 
 30035 
 30036 /* Return the index of the first glyph preceding glyph string S that
 30037    is overwriting S because of its right overhang.  Value is -1 if no
 30038    glyph in front of S overwrites S.  */
 30039 
 30040 static int
 30041 left_overwriting (struct glyph_string *s)
 30042 {
 30043   int i, k, x;
 30044   struct glyph *glyphs = s->row->glyphs[s->area];
 30045   int first = s->first_glyph - glyphs;
 30046 
 30047   k = -1;
 30048   x = 0;
 30049   for (i = first - 1; i >= 0; --i)
 30050     {
 30051       int left, right;
 30052       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30053       if (x + right > 0)
 30054         k = i;
 30055       x -= glyphs[i].pixel_width;
 30056     }
 30057 
 30058   return k;
 30059 }
 30060 
 30061 
 30062 /* Return the index of the last glyph following glyph string S that is
 30063    overwritten by S because of S's right overhang.  Value is -1 if
 30064    no such glyph is found.  */
 30065 
 30066 static int
 30067 right_overwritten (struct glyph_string *s)
 30068 {
 30069   int k = -1;
 30070 
 30071   if (s->right_overhang)
 30072     {
 30073       int x = 0, i;
 30074       struct glyph *glyphs = s->row->glyphs[s->area];
 30075       int first = (s->first_glyph - glyphs
 30076                    + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30077       int end = s->row->used[s->area];
 30078 
 30079       for (i = first; i < end && s->right_overhang > x; ++i)
 30080         x += glyphs[i].pixel_width;
 30081 
 30082       k = i;
 30083     }
 30084 
 30085   return k;
 30086 }
 30087 
 30088 
 30089 /* Return the index of the last glyph following glyph string S that
 30090    overwrites S because of its left overhang.  Value is negative
 30091    if no such glyph is found.  */
 30092 
 30093 static int
 30094 right_overwriting (struct glyph_string *s)
 30095 {
 30096   int i, k, x;
 30097   int end = s->row->used[s->area];
 30098   struct glyph *glyphs = s->row->glyphs[s->area];
 30099   int first = (s->first_glyph - glyphs
 30100                + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
 30101 
 30102   k = -1;
 30103   x = 0;
 30104   for (i = first; i < end; ++i)
 30105     {
 30106       int left, right;
 30107       gui_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
 30108       if (x - left < 0)
 30109         k = i;
 30110       x += glyphs[i].pixel_width;
 30111     }
 30112 
 30113   return k;
 30114 }
 30115 
 30116 
 30117 /* Set background width of glyph string S.  START is the index of the
 30118    first glyph following S.  LAST_X is the right-most x-position + 1
 30119    in the drawing area.
 30120 
 30121    If S->hl is DRAW_CURSOR, S->f is a window system frame, and the
 30122    cursor in S's window is currently inside mouse face, also update
 30123    S->width to take into account potentially differing :box
 30124    properties between the original face and the mouse face.  */
 30125 
 30126 static void
 30127 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
 30128 {
 30129   /* If the face of this glyph string has to be drawn to the end of
 30130      the drawing area, set S->extends_to_end_of_line_p.  */
 30131 
 30132   if (start == s->row->used[s->area]
 30133       && ((s->row->fill_line_p
 30134            && (s->hl == DRAW_NORMAL_TEXT
 30135                || s->hl == DRAW_IMAGE_RAISED
 30136                || s->hl == DRAW_IMAGE_SUNKEN))
 30137           || s->hl == DRAW_MOUSE_FACE))
 30138     s->extends_to_end_of_line_p = true;
 30139 
 30140   /* If S extends its face to the end of the line, set its
 30141      background_width to the distance to the right edge of the drawing
 30142      area.  */
 30143   if (s->extends_to_end_of_line_p)
 30144     s->background_width = last_x - s->x + 1;
 30145   else
 30146     {
 30147       s->background_width = s->width;
 30148 #ifdef HAVE_WINDOW_SYSTEM
 30149       if (FRAME_WINDOW_P (s->f)
 30150           && s->hl == DRAW_CURSOR
 30151           && MATRIX_ROW (s->w->current_matrix,
 30152                          s->w->phys_cursor.vpos)->mouse_face_p
 30153           && cursor_in_mouse_face_p (s->w))
 30154         {
 30155           /* Adjust the background width of the glyph string, because
 30156              if the glyph's face has the :box attribute, its
 30157              pixel_width might be different when it's displayed in the
 30158              mouse-face, if that also has the :box attribute.  */
 30159           struct glyph *g = s->first_glyph;
 30160           struct face *regular_face = FACE_FROM_ID (s->f, g->face_id);
 30161           s->background_width +=
 30162             adjust_glyph_width_for_mouse_face (g, s->row, s->w,
 30163                                                regular_face, s->face);
 30164           /* S->width is probably worth adjusting here as well.  */
 30165           s->width = s->background_width;
 30166         }
 30167 #endif
 30168     }
 30169 }
 30170 
 30171 
 30172 /* Return glyph string that shares background with glyph string S and
 30173    whose `background_width' member has been set.  */
 30174 
 30175 static struct glyph_string *
 30176 glyph_string_containing_background_width (struct glyph_string *s)
 30177 {
 30178   if (s->cmp)
 30179     while (s->cmp_from)
 30180       s = s->prev;
 30181 
 30182   return s;
 30183 }
 30184 
 30185 
 30186 /* Compute overhangs and x-positions for glyph string S and its
 30187    predecessors, or successors.  X is the starting x-position for S.
 30188    BACKWARD_P means process predecessors.  */
 30189 
 30190 static void
 30191 compute_overhangs_and_x (struct glyph_string *s, int x, bool backward_p)
 30192 {
 30193   if (backward_p)
 30194     {
 30195       while (s)
 30196         {
 30197           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30198             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30199           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30200             x -= s->width;
 30201           s->x = x;
 30202           s = s->prev;
 30203         }
 30204     }
 30205   else
 30206     {
 30207       while (s)
 30208         {
 30209           if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
 30210             FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
 30211           s->x = x;
 30212           if (!s->cmp || s->cmp_to == s->cmp->glyph_len)
 30213             x += s->width;
 30214           s = s->next;
 30215         }
 30216     }
 30217 }
 30218 
 30219 
 30220 
 30221 /* The following macros are only called from draw_glyphs below.
 30222    They reference the following parameters of that function directly:
 30223      `w', `row', `area', and `overlap_p'
 30224    as well as the following local variables:
 30225      `s', `f', and `hdc' (in W32)  */
 30226 
 30227 #ifdef HAVE_NTGUI
 30228 /* On W32, silently add local `hdc' variable to argument list of
 30229    init_glyph_string.  */
 30230 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30231   init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
 30232 #else
 30233 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
 30234   init_glyph_string (s, char2b, w, row, area, start, hl)
 30235 #endif
 30236 
 30237 /* Add a glyph string for a stretch glyph to the list of strings
 30238    between HEAD and TAIL.  START is the index of the stretch glyph in
 30239    row area AREA of glyph row ROW.  END is the index of the last glyph
 30240    in that glyph row area.  X is the current output position assigned
 30241    to the new glyph string constructed.  HL overrides that face of the
 30242    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30243    is the right-most x-position of the drawing area.  */
 30244 
 30245 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
 30246    and below -- keep them on one line.  */
 30247 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30248      do                                                                     \
 30249        {                                                                    \
 30250          s = alloca (sizeof *s);                                            \
 30251          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);              \
 30252          START = fill_stretch_glyph_string (s, START, END);                 \
 30253          append_glyph_string (&HEAD, &TAIL, s);                             \
 30254          s->x = (X);                                                        \
 30255        }                                                                    \
 30256      while (false)
 30257 
 30258 
 30259 /* Add a glyph string for an image glyph to the list of strings
 30260    between HEAD and TAIL.  START is the index of the image glyph in
 30261    row area AREA of glyph row ROW.  END is the index of the last glyph
 30262    in that glyph row area.  X is the current output position assigned
 30263    to the new glyph string constructed.  HL overrides that face of the
 30264    glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn.  LAST_X
 30265    is the right-most x-position of the drawing area.  */
 30266 
 30267 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30268      do                                                                 \
 30269        {                                                                \
 30270          s = alloca (sizeof *s);                                        \
 30271          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30272          fill_image_glyph_string (s);                                   \
 30273          append_glyph_string (&HEAD, &TAIL, s);                         \
 30274          ++START;                                                       \
 30275          s->x = (X);                                                    \
 30276        }                                                                \
 30277      while (false)
 30278 
 30279 #ifndef HAVE_XWIDGETS
 30280 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30281      eassume (false)
 30282 #else
 30283 # define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30284      do                                                                 \
 30285        {                                                                \
 30286          s = alloca (sizeof *s);                                        \
 30287          INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
 30288          fill_xwidget_glyph_string (s);                                 \
 30289          append_glyph_string (&(HEAD), &(TAIL), s);                     \
 30290          ++(START);                                                     \
 30291          s->x = (X);                                                    \
 30292        }                                                                \
 30293      while (false)
 30294 #endif
 30295 
 30296 /* Add a glyph string for a sequence of character glyphs to the list
 30297    of strings between HEAD and TAIL.  START is the index of the first
 30298    glyph in row area AREA of glyph row ROW that is part of the new
 30299    glyph string.  END is the index of the last glyph in that glyph row
 30300    area.  X is the current output position assigned to the new glyph
 30301    string constructed.  HL overrides that face of the glyph; e.g. it
 30302    is DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the
 30303    right-most x-position of the drawing area.  */
 30304 
 30305 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30306      do                                                                    \
 30307        {                                                                   \
 30308          int face_id;                                                      \
 30309          unsigned *char2b;                                         \
 30310                                                                            \
 30311          face_id = (row)->glyphs[area][START].face_id;                     \
 30312                                                                            \
 30313          s = alloca (sizeof *s);                                           \
 30314          SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
 30315          INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
 30316          append_glyph_string (&HEAD, &TAIL, s);                            \
 30317          s->x = (X);                                                       \
 30318          START = fill_glyph_string (s, face_id, START, END, overlaps);     \
 30319        }                                                                   \
 30320      while (false)
 30321 
 30322 
 30323 /* Add a glyph string for a composite sequence to the list of strings
 30324    between HEAD and TAIL.  START is the index of the first glyph in
 30325    row area AREA of glyph row ROW that is part of the new glyph
 30326    string.  END is the index of the last glyph in that glyph row area.
 30327    X is the current output position assigned to the new glyph string
 30328    constructed.  HL overrides that face of the glyph; e.g. it is
 30329    DRAW_CURSOR if a cursor has to be drawn.  LAST_X is the right-most
 30330    x-position of the drawing area.  */
 30331 
 30332 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30333   do {                                                                      \
 30334     int face_id = (row)->glyphs[area][START].face_id;                       \
 30335     struct face *base_face = FACE_FROM_ID (f, face_id);             \
 30336     ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id;                 \
 30337     struct composition *cmp = composition_table[cmp_id];                    \
 30338     unsigned *char2b;                                                       \
 30339     struct glyph_string *first_s = NULL;                                    \
 30340     int n;                                                                  \
 30341                                                                             \
 30342     SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                               \
 30343                                                                             \
 30344     /* Make glyph_strings for each glyph sequence that is drawable by       \
 30345        the same face, and append them to HEAD/TAIL.  */                     \
 30346     for (n = 0; n < cmp->glyph_len;)                                        \
 30347       {                                                                     \
 30348         s = alloca (sizeof *s);                                             \
 30349         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);             \
 30350         append_glyph_string (&(HEAD), &(TAIL), s);                          \
 30351         s->cmp = cmp;                                                       \
 30352         s->cmp_from = n;                                                    \
 30353         s->x = (X);                                                         \
 30354         if (n == 0)                                                         \
 30355           first_s = s;                                                      \
 30356         n = fill_composite_glyph_string (s, base_face, overlaps);           \
 30357       }                                                                     \
 30358                                                                             \
 30359     ++START;                                                                \
 30360     s = first_s;                                                            \
 30361   } while (false)
 30362 
 30363 
 30364 /* Add a glyph string for a glyph-string sequence to the list of strings
 30365    between HEAD and TAIL.  */
 30366 
 30367 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30368   do {                                                                    \
 30369     int face_id;                                                          \
 30370     unsigned *char2b;                                                     \
 30371     Lisp_Object gstring;                                                  \
 30372                                                                           \
 30373     face_id = (row)->glyphs[area][START].face_id;                         \
 30374     gstring = (composition_gstring_from_id                                \
 30375                ((row)->glyphs[area][START].u.cmp.id));                    \
 30376     s = alloca (sizeof *s);                                               \
 30377     SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));               \
 30378     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);               \
 30379     append_glyph_string (&(HEAD), &(TAIL), s);                            \
 30380     s->x = (X);                                                           \
 30381     START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
 30382   } while (false)
 30383 
 30384 
 30385 /* Add a glyph string for a sequence of glyphless character's glyphs
 30386    to the list of strings between HEAD and TAIL.  The meanings of
 30387    arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS.  */
 30388 
 30389 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
 30390   do                                                                        \
 30391     {                                                                       \
 30392       int face_id;                                                          \
 30393                                                                             \
 30394       face_id = (row)->glyphs[area][START].face_id;                         \
 30395                                                                             \
 30396       s = alloca (sizeof *s);                                               \
 30397       INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);                 \
 30398       append_glyph_string (&HEAD, &TAIL, s);                                \
 30399       s->x = (X);                                                           \
 30400       START = fill_glyphless_glyph_string (s, face_id, START, END,          \
 30401                                            overlaps);                       \
 30402     }                                                                       \
 30403   while (false)
 30404 
 30405 
 30406 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
 30407    of AREA of glyph row ROW on window W between indices START and END.
 30408    HL overrides the face for drawing glyph strings, e.g. it is
 30409    DRAW_CURSOR to draw a cursor.  X and LAST_X are start and end
 30410    x-positions of the drawing area.
 30411 
 30412    This is an ugly monster macro construct because we must use alloca
 30413    to allocate glyph strings (because draw_glyphs can be called
 30414    asynchronously).  */
 30415 
 30416 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30417   do                                                                    \
 30418     {                                                                   \
 30419       HEAD = TAIL = NULL;                                               \
 30420       while (START < END)                                               \
 30421         {                                                               \
 30422           struct glyph *first_glyph = (row)->glyphs[area] + START;      \
 30423           switch (first_glyph->type)                                    \
 30424             {                                                           \
 30425             case CHAR_GLYPH:                                            \
 30426               BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL,         \
 30427                                         HL, X, LAST_X);                 \
 30428               break;                                                    \
 30429                                                                         \
 30430             case COMPOSITE_GLYPH:                                       \
 30431               if (first_glyph->u.cmp.automatic)                         \
 30432                 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30433                                             HL, X, LAST_X);             \
 30434               else                                                      \
 30435                 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL,   \
 30436                                               HL, X, LAST_X);           \
 30437               break;                                                    \
 30438                                                                         \
 30439             case STRETCH_GLYPH:                                         \
 30440               BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30441                                           HL, X, LAST_X);               \
 30442               break;                                                    \
 30443                                                                         \
 30444             case IMAGE_GLYPH:                                           \
 30445               BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
 30446                                         HL, X, LAST_X);                 \
 30447               break;
 30448 
 30449 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)   \
 30450             case XWIDGET_GLYPH:                                         \
 30451               BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
 30452                                           HL, X, LAST_X);               \
 30453               break;
 30454 
 30455 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)    \
 30456             case GLYPHLESS_GLYPH:                                       \
 30457               BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
 30458                                             HL, X, LAST_X);             \
 30459               break;                                                    \
 30460                                                                         \
 30461             default:                                                    \
 30462               emacs_abort ();                                           \
 30463             }                                                           \
 30464                                                                         \
 30465           if (s)                                                        \
 30466             {                                                           \
 30467               set_glyph_string_background_width (s, START, LAST_X);     \
 30468               (X) += s->width;                                          \
 30469             }                                                           \
 30470         }                                                               \
 30471     } while (false)
 30472 
 30473 
 30474 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)      \
 30475     BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)        \
 30476     BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)       \
 30477     BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
 30478 
 30479 
 30480 /* Draw glyphs between START and END in AREA of ROW on window W,
 30481    starting at x-position X.  X is relative to AREA in W.  HL is a
 30482    face-override with the following meaning:
 30483 
 30484    DRAW_NORMAL_TEXT     draw normally
 30485    DRAW_CURSOR          draw in cursor face
 30486    DRAW_MOUSE_FACE      draw in mouse face.
 30487    DRAW_INVERSE_VIDEO   draw in mode line face
 30488    DRAW_IMAGE_SUNKEN    draw an image with a sunken relief around it
 30489    DRAW_IMAGE_RAISED    draw an image with a raised relief around it
 30490 
 30491    If OVERLAPS is non-zero, draw only the foreground of characters and
 30492    clip to the physical height of ROW.  Non-zero value also defines
 30493    the overlapping part to be drawn:
 30494 
 30495    OVERLAPS_PRED                overlap with preceding rows
 30496    OVERLAPS_SUCC                overlap with succeeding rows
 30497    OVERLAPS_BOTH                overlap with both preceding/succeeding rows
 30498    OVERLAPS_ERASED_CURSOR       overlap with erased cursor area
 30499 
 30500    Value is the x-position reached, relative to AREA of W.  */
 30501 
 30502 static int
 30503 draw_glyphs (struct window *w, int x, struct glyph_row *row,
 30504              enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
 30505              enum draw_glyphs_face hl, int overlaps)
 30506 {
 30507   struct glyph_string *head, *tail;
 30508   struct glyph_string *s;
 30509   struct glyph_string *clip_head = NULL, *clip_tail = NULL;
 30510   int i, j, x_reached, last_x, area_left = 0;
 30511   struct frame *f = XFRAME (WINDOW_FRAME (w));
 30512 
 30513   ALLOCATE_HDC (hdc, f);
 30514 
 30515   /* Let's rather be paranoid than getting a SEGV.  */
 30516   end = min (end, row->used[area]);
 30517   start = clip_to_bounds (0, start, end);
 30518 
 30519   /* Translate X to frame coordinates.  Set last_x to the right
 30520      end of the drawing area.  */
 30521   if (row->full_width_p)
 30522     {
 30523       /* X is relative to the left edge of W, without scroll bars
 30524          or fringes.  */
 30525       area_left = WINDOW_LEFT_EDGE_X (w);
 30526       last_x = (WINDOW_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w)
 30527                 - (row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 30528     }
 30529   else
 30530     {
 30531       area_left = window_box_left (w, area);
 30532       last_x = area_left + window_box_width (w, area);
 30533     }
 30534   x += area_left;
 30535 
 30536   /* Build a doubly-linked list of glyph_string structures between
 30537      head and tail from what we have to draw.  Note that the macro
 30538      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
 30539      the reason we use a separate variable `i'.  */
 30540   i = start;
 30541   USE_SAFE_ALLOCA;
 30542   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
 30543   if (tail)
 30544     {
 30545       s = glyph_string_containing_background_width (tail);
 30546       x_reached = s->x + s->background_width;
 30547     }
 30548   else
 30549     x_reached = x;
 30550 
 30551   /* If there are any glyphs with lbearing < 0 or rbearing > width in
 30552      the row, redraw some glyphs in front or following the glyph
 30553      strings built above.  */
 30554   if (head && !overlaps && row->contains_overlapping_glyphs_p)
 30555     {
 30556       struct glyph_string *h, *t;
 30557       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 30558       int mouse_beg_col UNINIT, mouse_end_col UNINIT;
 30559       bool check_mouse_face = false;
 30560       int dummy_x = 0;
 30561 
 30562       /* If mouse highlighting is on, we may need to draw adjacent
 30563          glyphs using mouse-face highlighting.  */
 30564       if (area == TEXT_AREA && row->mouse_face_p
 30565           && hlinfo->mouse_face_beg_row >= 0
 30566           && hlinfo->mouse_face_end_row >= 0)
 30567         {
 30568           ptrdiff_t row_vpos = MATRIX_ROW_VPOS (row, w->current_matrix);
 30569 
 30570           if (row_vpos >= hlinfo->mouse_face_beg_row
 30571               && row_vpos <= hlinfo->mouse_face_end_row)
 30572             {
 30573               check_mouse_face = true;
 30574               mouse_beg_col = (row_vpos == hlinfo->mouse_face_beg_row)
 30575                 ? hlinfo->mouse_face_beg_col : 0;
 30576               mouse_end_col = (row_vpos == hlinfo->mouse_face_end_row)
 30577                 ? hlinfo->mouse_face_end_col
 30578                 : row->used[TEXT_AREA];
 30579             }
 30580         }
 30581 
 30582       /* Compute overhangs for all glyph strings.  */
 30583       if (FRAME_RIF (f)->compute_glyph_string_overhangs)
 30584         for (s = head; s; s = s->next)
 30585           FRAME_RIF (f)->compute_glyph_string_overhangs (s);
 30586 
 30587       /* Prepend glyph strings for glyphs in front of the first glyph
 30588          string that are overwritten because of the first glyph
 30589          string's left overhang.  The background of all strings
 30590          prepended must be drawn because the first glyph string
 30591          draws over it.  */
 30592       i = left_overwritten (head);
 30593       if (i >= 0)
 30594         {
 30595           enum draw_glyphs_face overlap_hl;
 30596 
 30597           /* If this row contains mouse highlighting, attempt to draw
 30598              the overlapped glyphs with the correct highlight.  This
 30599              code fails if the overlap encompasses more than one glyph
 30600              and mouse-highlight spans only some of these glyphs.
 30601              However, making it work perfectly involves a lot more
 30602              code, and I don't know if the pathological case occurs in
 30603              practice, so we'll stick to this for now.  --- cyd  */
 30604           if (check_mouse_face
 30605               && mouse_beg_col < start && mouse_end_col > i)
 30606             overlap_hl = DRAW_MOUSE_FACE;
 30607           else
 30608             overlap_hl = DRAW_NORMAL_TEXT;
 30609 
 30610           if (hl != overlap_hl)
 30611             clip_head = head;
 30612           j = i;
 30613           BUILD_GLYPH_STRINGS (j, start, h, t,
 30614                                overlap_hl, dummy_x, last_x);
 30615           start = i;
 30616           compute_overhangs_and_x (t, head->x, true);
 30617           prepend_glyph_string_lists (&head, &tail, h, t);
 30618           if (clip_head == NULL)
 30619             clip_head = head;
 30620         }
 30621 
 30622       /* Prepend glyph strings for glyphs in front of the first glyph
 30623          string that overwrite that glyph string because of their
 30624          right overhang.  For these strings, only the foreground must
 30625          be drawn, because it draws over the glyph string at `head'.
 30626          The background must not be drawn because this would overwrite
 30627          right overhangs of preceding glyphs for which no glyph
 30628          strings exist.  */
 30629       i = left_overwriting (head);
 30630       if (i >= 0)
 30631         {
 30632           enum draw_glyphs_face overlap_hl;
 30633 
 30634           if (check_mouse_face
 30635               && mouse_beg_col < start && mouse_end_col > i)
 30636             overlap_hl = DRAW_MOUSE_FACE;
 30637           else
 30638             overlap_hl = DRAW_NORMAL_TEXT;
 30639 
 30640           if (hl == overlap_hl || clip_head == NULL)
 30641             clip_head = head;
 30642           BUILD_GLYPH_STRINGS (i, start, h, t,
 30643                                overlap_hl, dummy_x, last_x);
 30644           for (s = h; s; s = s->next)
 30645             s->background_filled_p = true;
 30646           compute_overhangs_and_x (t, head->x, true);
 30647           prepend_glyph_string_lists (&head, &tail, h, t);
 30648         }
 30649 
 30650       /* Append glyphs strings for glyphs following the last glyph
 30651          string tail that are overwritten by tail.  The background of
 30652          these strings has to be drawn because tail's foreground draws
 30653          over it.  */
 30654       i = right_overwritten (tail);
 30655       if (i >= 0)
 30656         {
 30657           enum draw_glyphs_face overlap_hl;
 30658 
 30659           if (check_mouse_face
 30660               && mouse_beg_col < i && mouse_end_col > end)
 30661             overlap_hl = DRAW_MOUSE_FACE;
 30662           else
 30663             overlap_hl = DRAW_NORMAL_TEXT;
 30664 
 30665           if (hl != overlap_hl)
 30666             clip_tail = tail;
 30667           BUILD_GLYPH_STRINGS (end, i, h, t,
 30668                                overlap_hl, x, last_x);
 30669           /* Because BUILD_GLYPH_STRINGS updates the first argument,
 30670              we don't have `end = i;' here.  */
 30671           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30672           append_glyph_string_lists (&head, &tail, h, t);
 30673           if (clip_tail == NULL)
 30674             clip_tail = tail;
 30675         }
 30676 
 30677       /* Append glyph strings for glyphs following the last glyph
 30678          string tail that overwrite tail.  The foreground of such
 30679          glyphs has to be drawn because it writes into the background
 30680          of tail.  The background must not be drawn because it could
 30681          paint over the foreground of following glyphs.  */
 30682       i = right_overwriting (tail);
 30683       if (i >= 0)
 30684         {
 30685           enum draw_glyphs_face overlap_hl;
 30686           if (check_mouse_face
 30687               && mouse_beg_col < i && mouse_end_col > end)
 30688             overlap_hl = DRAW_MOUSE_FACE;
 30689           else
 30690             overlap_hl = DRAW_NORMAL_TEXT;
 30691 
 30692           if (hl == overlap_hl || clip_tail == NULL)
 30693             clip_tail = tail;
 30694           i++;                  /* We must include the Ith glyph.  */
 30695           BUILD_GLYPH_STRINGS (end, i, h, t,
 30696                                overlap_hl, x, last_x);
 30697           for (s = h; s; s = s->next)
 30698             s->background_filled_p = true;
 30699           compute_overhangs_and_x (h, tail->x + tail->width, false);
 30700           append_glyph_string_lists (&head, &tail, h, t);
 30701         }
 30702       tail = glyph_string_containing_background_width (tail);
 30703       if (clip_tail)
 30704         clip_tail = glyph_string_containing_background_width (clip_tail);
 30705       if (clip_head || clip_tail)
 30706         for (s = head; s; s = s->next)
 30707           {
 30708             s->clip_head = clip_head;
 30709             s->clip_tail = clip_tail;
 30710           }
 30711     }
 30712 
 30713   /* Draw all strings.  */
 30714   for (s = head; s; s = s->next)
 30715     FRAME_RIF (f)->draw_glyph_string (s);
 30716 
 30717   /* When focus a sole frame and move horizontally, this clears on_p
 30718      causing a failure to erase prev cursor position. */
 30719   if (area == TEXT_AREA
 30720       && !row->full_width_p
 30721       /* When drawing overlapping rows, only the glyph strings'
 30722          foreground is drawn, which doesn't erase a cursor
 30723          completely. */
 30724       && !overlaps)
 30725     {
 30726       int x0 = clip_head ? clip_head->x : (head ? head->x : x);
 30727       int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
 30728                 : (tail ? tail->x + tail->background_width : x));
 30729       x0 -= area_left;
 30730       x1 -= area_left;
 30731 
 30732       notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
 30733                                  row->y, MATRIX_ROW_BOTTOM_Y (row));
 30734     }
 30735 
 30736   /* Value is the x-position up to which drawn, relative to AREA of W.
 30737      This doesn't include parts drawn because of overhangs.  */
 30738   if (row->full_width_p)
 30739     x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
 30740   else
 30741     x_reached -= area_left;
 30742 
 30743   RELEASE_HDC (hdc, f);
 30744 
 30745   SAFE_FREE ();
 30746   return x_reached;
 30747 }
 30748 
 30749 /* Find the first glyph in the run of underlined glyphs preceding the
 30750    beginning of glyph string S, and return its font (which could be
 30751    NULL).  This is needed because that font determines the underline
 30752    position and thickness for the entire run of the underlined glyphs.
 30753    This function is called from the draw_glyph_string method of GUI
 30754    frame's redisplay interface (RIF) when it needs to draw in an
 30755    underlined face.  */
 30756 struct font *
 30757 font_for_underline_metrics (struct glyph_string *s)
 30758 {
 30759   struct glyph *g0 = s->row->glyphs[s->area], *g;
 30760 
 30761   for (g = s->first_glyph - 1; g >= g0; g--)
 30762     {
 30763       struct face *prev_face = FACE_FROM_ID (s->f, g->face_id);
 30764       if (!(prev_face && prev_face->underline != FACE_NO_UNDERLINE))
 30765         break;
 30766     }
 30767 
 30768   /* If preceding glyphs are not underlined, use the font of S.  */
 30769   if (g == s->first_glyph - 1)
 30770     return s->font;
 30771   else
 30772     {
 30773       /* Otherwise use the font of the last glyph we saw in the above
 30774          loop whose face had the underline_p flag set.  */
 30775       return FACE_FROM_ID (s->f, g[1].face_id)->font;
 30776     }
 30777 }
 30778 
 30779 /* Expand row matrix if too narrow.  Don't expand if area
 30780    is not present.  */
 30781 
 30782 #define IT_EXPAND_MATRIX_WIDTH(it, area)                \
 30783   {                                                     \
 30784     if (!it->f->fonts_changed                           \
 30785         && (it->glyph_row->glyphs[area]                 \
 30786             < it->glyph_row->glyphs[area + 1]))         \
 30787       {                                                 \
 30788         it->w->ncols_scale_factor++;                    \
 30789         it->f->fonts_changed = true;                    \
 30790       }                                                 \
 30791   }
 30792 
 30793 /* Store one glyph for IT->char_to_display in IT->glyph_row.
 30794    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30795 
 30796 static void
 30797 append_glyph (struct it *it)
 30798 {
 30799   struct glyph *glyph;
 30800   enum glyph_row_area area = it->area;
 30801 
 30802   eassert (it->glyph_row);
 30803   eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
 30804 
 30805   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30806   if (glyph < it->glyph_row->glyphs[area + 1])
 30807     {
 30808       /* If the glyph row is reversed, we need to prepend the glyph
 30809          rather than append it.  */
 30810       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30811         {
 30812           struct glyph *g;
 30813 
 30814           /* Make room for the additional glyph.  */
 30815           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 30816             g[1] = *g;
 30817           glyph = it->glyph_row->glyphs[area];
 30818         }
 30819       glyph->charpos = CHARPOS (it->position);
 30820       glyph->object = it->object;
 30821       if (it->pixel_width > 0)
 30822         {
 30823           eassert (it->pixel_width <= SHRT_MAX);
 30824           glyph->pixel_width = it->pixel_width;
 30825           glyph->padding_p = false;
 30826         }
 30827       else
 30828         {
 30829           /* Assure at least 1-pixel width.  Otherwise, cursor can't
 30830              be displayed correctly.  */
 30831           glyph->pixel_width = 1;
 30832           glyph->padding_p = true;
 30833         }
 30834       glyph->ascent = it->ascent;
 30835       glyph->descent = it->descent;
 30836       glyph->voffset = it->voffset;
 30837       glyph->type = CHAR_GLYPH;
 30838       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30839       glyph->multibyte_p = it->multibyte_p;
 30840       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30841         {
 30842           /* In R2L rows, the left and the right box edges need to be
 30843              drawn in reverse direction.  */
 30844           glyph->right_box_line_p = it->start_of_box_run_p;
 30845           glyph->left_box_line_p = it->end_of_box_run_p;
 30846         }
 30847       else
 30848         {
 30849           glyph->left_box_line_p = it->start_of_box_run_p;
 30850           glyph->right_box_line_p = it->end_of_box_run_p;
 30851         }
 30852       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30853                                       || it->phys_descent > it->descent);
 30854       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30855       glyph->face_id = it->face_id;
 30856       glyph->u.ch = it->char_to_display;
 30857       glyph->slice.img = null_glyph_slice;
 30858       glyph->font_type = FONT_TYPE_UNKNOWN;
 30859       if (it->bidi_p)
 30860         {
 30861           glyph->resolved_level = it->bidi_it.resolved_level;
 30862           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30863           glyph->bidi_type = it->bidi_it.type;
 30864         }
 30865       else
 30866         {
 30867           glyph->resolved_level = 0;
 30868           glyph->bidi_type = UNKNOWN_BT;
 30869         }
 30870       ++it->glyph_row->used[area];
 30871     }
 30872   else
 30873     IT_EXPAND_MATRIX_WIDTH (it, area);
 30874 }
 30875 
 30876 /* Store one glyph for the composition IT->cmp_it.id in IT->glyph_row.
 30877    Called from gui_produce_glyphs when IT->glyph_row is non-null.  */
 30878 
 30879 static void
 30880 append_composite_glyph (struct it *it)
 30881 {
 30882   struct glyph *glyph;
 30883   enum glyph_row_area area = it->area;
 30884 
 30885   eassert (it->glyph_row);
 30886 
 30887   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 30888   if (glyph < it->glyph_row->glyphs[area + 1])
 30889     {
 30890       /* If the glyph row is reversed, we need to prepend the glyph
 30891          rather than append it.  */
 30892       if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
 30893         {
 30894           struct glyph *g;
 30895 
 30896           /* Make room for the new glyph.  */
 30897           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 30898             g[1] = *g;
 30899           glyph = it->glyph_row->glyphs[it->area];
 30900         }
 30901       glyph->charpos = it->cmp_it.charpos;
 30902       glyph->object = it->object;
 30903       eassert (it->pixel_width <= SHRT_MAX);
 30904       glyph->pixel_width = it->pixel_width;
 30905       glyph->ascent = it->ascent;
 30906       glyph->descent = it->descent;
 30907       glyph->voffset = it->voffset;
 30908       glyph->type = COMPOSITE_GLYPH;
 30909       if (it->cmp_it.ch < 0)
 30910         {
 30911           glyph->u.cmp.automatic = false;
 30912           glyph->u.cmp.id = it->cmp_it.id;
 30913           glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
 30914         }
 30915       else
 30916         {
 30917           glyph->u.cmp.automatic = true;
 30918           glyph->u.cmp.id = it->cmp_it.id;
 30919           glyph->slice.cmp.from = it->cmp_it.from;
 30920           glyph->slice.cmp.to = it->cmp_it.to - 1;
 30921         }
 30922       glyph->avoid_cursor_p = it->avoid_cursor_p;
 30923       glyph->multibyte_p = it->multibyte_p;
 30924       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 30925         {
 30926           /* In R2L rows, the left and the right box edges need to be
 30927              drawn in reverse direction.  */
 30928           glyph->right_box_line_p = it->start_of_box_run_p;
 30929           glyph->left_box_line_p = it->end_of_box_run_p;
 30930         }
 30931       else
 30932         {
 30933           glyph->left_box_line_p = it->start_of_box_run_p;
 30934           glyph->right_box_line_p = it->end_of_box_run_p;
 30935         }
 30936       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 30937                                       || it->phys_descent > it->descent);
 30938       glyph->padding_p = false;
 30939       glyph->glyph_not_available_p = it->glyph_not_available_p;
 30940       glyph->face_id = it->face_id;
 30941       glyph->font_type = FONT_TYPE_UNKNOWN;
 30942       if (it->bidi_p)
 30943         {
 30944           glyph->resolved_level = it->bidi_it.resolved_level;
 30945           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 30946           glyph->bidi_type = it->bidi_it.type;
 30947         }
 30948       ++it->glyph_row->used[area];
 30949     }
 30950   else
 30951     IT_EXPAND_MATRIX_WIDTH (it, area);
 30952 }
 30953 
 30954 
 30955 /* Change IT->ascent and IT->height according to the setting of
 30956    IT->voffset.  */
 30957 
 30958 static void
 30959 take_vertical_position_into_account (struct it *it)
 30960 {
 30961   if (it->voffset)
 30962     {
 30963       if (it->voffset < 0)
 30964         /* Increase the ascent so that we can display the text higher
 30965            in the line.  */
 30966         it->ascent -= it->voffset;
 30967       else
 30968         /* Increase the descent so that we can display the text lower
 30969            in the line.  */
 30970         it->descent += it->voffset;
 30971     }
 30972 }
 30973 
 30974 
 30975 /* Produce glyphs/get display metrics for the image IT is loaded with.
 30976    See the description of struct display_iterator in dispextern.h for
 30977    an overview of struct display_iterator.  */
 30978 
 30979 static void
 30980 produce_image_glyph (struct it *it)
 30981 {
 30982   struct image *img;
 30983   struct face *face;
 30984   int glyph_ascent, crop;
 30985   struct glyph_slice slice;
 30986 
 30987   eassert (it->what == IT_IMAGE);
 30988 
 30989   face = FACE_FROM_ID (it->f, it->face_id);
 30990   /* Make sure X resources of the face is loaded.  */
 30991   prepare_face_for_display (it->f, face);
 30992 
 30993   if (it->image_id < 0)
 30994     {
 30995       /* Fringe bitmap.  */
 30996       it->ascent = it->phys_ascent = 0;
 30997       it->descent = it->phys_descent = 0;
 30998       it->pixel_width = 0;
 30999       it->nglyphs = 0;
 31000       return;
 31001     }
 31002 
 31003   img = IMAGE_FROM_ID (it->f, it->image_id);
 31004   /* Make sure X resources of the image is loaded.  */
 31005   prepare_image_for_display (it->f, img);
 31006 
 31007   slice.x = slice.y = 0;
 31008   slice.width = img->width;
 31009   slice.height = img->height;
 31010 
 31011   if (FIXNUMP (it->slice.x))
 31012     slice.x = XFIXNUM (it->slice.x);
 31013   else if (FLOATP (it->slice.x))
 31014     slice.x = XFLOAT_DATA (it->slice.x) * img->width;
 31015 
 31016   if (FIXNUMP (it->slice.y))
 31017     slice.y = XFIXNUM (it->slice.y);
 31018   else if (FLOATP (it->slice.y))
 31019     slice.y = XFLOAT_DATA (it->slice.y) * img->height;
 31020 
 31021   if (FIXNUMP (it->slice.width))
 31022     slice.width = XFIXNUM (it->slice.width);
 31023   else if (FLOATP (it->slice.width))
 31024     slice.width = XFLOAT_DATA (it->slice.width) * img->width;
 31025 
 31026   if (FIXNUMP (it->slice.height))
 31027     slice.height = XFIXNUM (it->slice.height);
 31028   else if (FLOATP (it->slice.height))
 31029     slice.height = XFLOAT_DATA (it->slice.height) * img->height;
 31030 
 31031   if (slice.x >= img->width)
 31032     slice.x = img->width;
 31033   if (slice.y >= img->height)
 31034     slice.y = img->height;
 31035   if (slice.x + slice.width >= img->width)
 31036     slice.width = img->width - slice.x;
 31037   if (slice.y + slice.height > img->height)
 31038     slice.height = img->height - slice.y;
 31039 
 31040   if (slice.width == 0 || slice.height == 0)
 31041     return;
 31042 
 31043   it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
 31044 
 31045   it->descent = slice.height - glyph_ascent;
 31046   if (slice.y == 0)
 31047     it->descent += img->vmargin;
 31048   if (slice.y + slice.height == img->height)
 31049     it->descent += img->vmargin;
 31050   it->phys_descent = it->descent;
 31051 
 31052   it->pixel_width = slice.width;
 31053   if (slice.x == 0)
 31054     it->pixel_width += img->hmargin;
 31055   if (slice.x + slice.width == img->width)
 31056     it->pixel_width += img->hmargin;
 31057 
 31058   /* It's quite possible for images to have an ascent greater than
 31059      their height, so don't get confused in that case.  */
 31060   if (it->descent < 0)
 31061     it->descent = 0;
 31062 
 31063   it->nglyphs = 1;
 31064 
 31065   if (face->box != FACE_NO_BOX)
 31066     {
 31067       /* If you change the logic here, please change it in
 31068          get_cursor_offset_for_mouse_face as well. */
 31069       if (face->box_horizontal_line_width > 0)
 31070         {
 31071           if (slice.y == 0)
 31072             it->ascent += face->box_horizontal_line_width;
 31073           if (slice.y + slice.height == img->height)
 31074             it->descent += face->box_horizontal_line_width;
 31075         }
 31076 
 31077       if (face->box_vertical_line_width > 0)
 31078         {
 31079           if (it->start_of_box_run_p && slice.x == 0)
 31080             it->pixel_width += face->box_vertical_line_width;
 31081           if (it->end_of_box_run_p && slice.x + slice.width == img->width)
 31082             it->pixel_width += face->box_vertical_line_width;
 31083         }
 31084     }
 31085 
 31086   take_vertical_position_into_account (it);
 31087 
 31088   /* Automatically crop wide image glyphs at right edge so we can
 31089      draw the cursor on same display row.  */
 31090   if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
 31091       && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31092     {
 31093       it->pixel_width -= crop;
 31094       slice.width -= crop;
 31095     }
 31096 
 31097   if (it->glyph_row)
 31098     {
 31099       struct glyph *glyph;
 31100       enum glyph_row_area area = it->area;
 31101 
 31102       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31103       if (it->glyph_row->reversed_p)
 31104         {
 31105           struct glyph *g;
 31106 
 31107           /* Make room for the new glyph.  */
 31108           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31109             g[1] = *g;
 31110           glyph = it->glyph_row->glyphs[it->area];
 31111         }
 31112       if (glyph < it->glyph_row->glyphs[area + 1])
 31113         {
 31114           glyph->charpos = CHARPOS (it->position);
 31115           glyph->object = it->object;
 31116           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31117           glyph->ascent = glyph_ascent;
 31118           glyph->descent = it->descent;
 31119           glyph->voffset = it->voffset;
 31120           glyph->type = IMAGE_GLYPH;
 31121           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31122           glyph->multibyte_p = it->multibyte_p;
 31123           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31124             {
 31125               /* In R2L rows, the left and the right box edges need to be
 31126                  drawn in reverse direction.  */
 31127               glyph->right_box_line_p = it->start_of_box_run_p;
 31128               glyph->left_box_line_p = it->end_of_box_run_p;
 31129             }
 31130           else
 31131             {
 31132               glyph->left_box_line_p = it->start_of_box_run_p;
 31133               glyph->right_box_line_p = it->end_of_box_run_p;
 31134             }
 31135           glyph->overlaps_vertically_p = false;
 31136           glyph->padding_p = false;
 31137           glyph->glyph_not_available_p = false;
 31138           glyph->face_id = it->face_id;
 31139           glyph->u.img_id = img->id;
 31140           glyph->slice.img = slice;
 31141           glyph->font_type = FONT_TYPE_UNKNOWN;
 31142           if (it->bidi_p)
 31143             {
 31144               glyph->resolved_level = it->bidi_it.resolved_level;
 31145               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31146               glyph->bidi_type = it->bidi_it.type;
 31147             }
 31148           ++it->glyph_row->used[area];
 31149         }
 31150       else
 31151         IT_EXPAND_MATRIX_WIDTH (it, area);
 31152     }
 31153 }
 31154 
 31155 static void
 31156 produce_xwidget_glyph (struct it *it)
 31157 {
 31158 #ifdef HAVE_XWIDGETS
 31159   struct xwidget *xw;
 31160   int glyph_ascent, crop;
 31161   eassert (it->what == IT_XWIDGET);
 31162 
 31163   struct face *face = FACE_FROM_ID (it->f, it->face_id);
 31164   /* Make sure X resources of the face is loaded.  */
 31165   prepare_face_for_display (it->f, face);
 31166 
 31167   xw = it->xwidget;
 31168   it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
 31169   it->descent = xw->height/2;
 31170   it->phys_descent = it->descent;
 31171   it->pixel_width = xw->width;
 31172   /* It's quite possible for images to have an ascent greater than
 31173      their height, so don't get confused in that case.  */
 31174   if (it->descent < 0)
 31175     it->descent = 0;
 31176 
 31177   it->nglyphs = 1;
 31178 
 31179   if (face->box != FACE_NO_BOX)
 31180     {
 31181       if (face->box_horizontal_line_width > 0)
 31182         {
 31183           it->ascent += face->box_horizontal_line_width;
 31184           it->descent += face->box_horizontal_line_width;
 31185         }
 31186 
 31187       if (face->box_vertical_line_width > 0)
 31188         {
 31189           if (it->start_of_box_run_p)
 31190             it->pixel_width += face->box_vertical_line_width;
 31191           it->pixel_width += face->box_vertical_line_width;
 31192         }
 31193     }
 31194 
 31195   take_vertical_position_into_account (it);
 31196 
 31197   /* Automatically crop wide image glyphs at right edge so we can
 31198      draw the cursor on same display row.  */
 31199   crop = it->pixel_width - (it->last_visible_x - it->current_x);
 31200   if (crop > 0 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
 31201     it->pixel_width -= crop;
 31202 
 31203   if (it->glyph_row)
 31204     {
 31205       enum glyph_row_area area = it->area;
 31206       struct glyph *glyph
 31207         = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31208 
 31209       if (it->glyph_row->reversed_p)
 31210         {
 31211           struct glyph *g;
 31212 
 31213           /* Make room for the new glyph.  */
 31214           for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
 31215             g[1] = *g;
 31216           glyph = it->glyph_row->glyphs[it->area];
 31217         }
 31218       if (glyph < it->glyph_row->glyphs[area + 1])
 31219         {
 31220           glyph->charpos = CHARPOS (it->position);
 31221           glyph->object = it->object;
 31222           glyph->pixel_width = clip_to_bounds (-1, it->pixel_width, SHRT_MAX);
 31223           glyph->ascent = glyph_ascent;
 31224           glyph->descent = it->descent;
 31225           glyph->voffset = it->voffset;
 31226           glyph->type = XWIDGET_GLYPH;
 31227           glyph->avoid_cursor_p = it->avoid_cursor_p;
 31228           glyph->multibyte_p = it->multibyte_p;
 31229           if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31230             {
 31231               /* In R2L rows, the left and the right box edges need to be
 31232                  drawn in reverse direction.  */
 31233               glyph->right_box_line_p = it->start_of_box_run_p;
 31234               glyph->left_box_line_p = it->end_of_box_run_p;
 31235             }
 31236           else
 31237             {
 31238               glyph->left_box_line_p = it->start_of_box_run_p;
 31239               glyph->right_box_line_p = it->end_of_box_run_p;
 31240             }
 31241           glyph->overlaps_vertically_p = 0;
 31242           glyph->padding_p = 0;
 31243           glyph->glyph_not_available_p = 0;
 31244           glyph->face_id = it->face_id;
 31245           glyph->u.xwidget = it->xwidget->xwidget_id;
 31246           glyph->font_type = FONT_TYPE_UNKNOWN;
 31247           if (it->bidi_p)
 31248             {
 31249               glyph->resolved_level = it->bidi_it.resolved_level;
 31250               eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31251               glyph->bidi_type = it->bidi_it.type;
 31252             }
 31253           ++it->glyph_row->used[area];
 31254         }
 31255       else
 31256         IT_EXPAND_MATRIX_WIDTH (it, area);
 31257     }
 31258 #endif
 31259 }
 31260 
 31261 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
 31262    of the glyph, WIDTH and HEIGHT are the width and height of the
 31263    stretch.  ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT).  */
 31264 
 31265 static void
 31266 append_stretch_glyph (struct it *it, Lisp_Object object,
 31267                       int width, int height, int ascent)
 31268 {
 31269   struct glyph *glyph;
 31270   enum glyph_row_area area = it->area;
 31271 
 31272   eassert (ascent >= 0 && ascent <= height);
 31273 
 31274   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31275   if (glyph < it->glyph_row->glyphs[area + 1])
 31276     {
 31277       /* If the glyph row is reversed, we need to prepend the glyph
 31278          rather than append it.  */
 31279       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31280         {
 31281           struct glyph *g;
 31282 
 31283           /* Make room for the additional glyph.  */
 31284           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31285             g[1] = *g;
 31286           glyph = it->glyph_row->glyphs[area];
 31287 
 31288           /* Decrease the width of the first glyph of the row that
 31289              begins before first_visible_x (e.g., due to hscroll).
 31290              This is so the overall width of the row becomes smaller
 31291              by the scroll amount, and the stretch glyph appended by
 31292              extend_face_to_end_of_line will be wider, to shift the
 31293              row glyphs to the right.  (In L2R rows, the corresponding
 31294              left-shift effect is accomplished by setting row->x to a
 31295              negative value, which won't work with R2L rows.)
 31296 
 31297              This must leave us with a positive value of WIDTH, since
 31298              otherwise the call to move_it_in_display_line_to at the
 31299              beginning of display_line would have got past the entire
 31300              first glyph, and then it->current_x would have been
 31301              greater or equal to it->first_visible_x.  */
 31302           if (it->current_x < it->first_visible_x)
 31303             width -= it->first_visible_x - it->current_x;
 31304           eassert (width > 0);
 31305         }
 31306       glyph->charpos = CHARPOS (it->position);
 31307       glyph->object = object;
 31308       /* FIXME: It would be better to use TYPE_MAX here, but
 31309          __typeof__ is not portable enough...  */
 31310       glyph->pixel_width = clip_to_bounds (-1, width, SHRT_MAX);
 31311       glyph->ascent = ascent;
 31312       glyph->descent = height - ascent;
 31313       glyph->voffset = it->voffset;
 31314       glyph->type = STRETCH_GLYPH;
 31315       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31316       glyph->multibyte_p = it->multibyte_p;
 31317       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31318         {
 31319           /* In R2L rows, the left and the right box edges need to be
 31320              drawn in reverse direction.  */
 31321           glyph->right_box_line_p = it->start_of_box_run_p;
 31322           glyph->left_box_line_p = it->end_of_box_run_p;
 31323         }
 31324       else
 31325         {
 31326           glyph->left_box_line_p = it->start_of_box_run_p;
 31327           glyph->right_box_line_p = it->end_of_box_run_p;
 31328         }
 31329       glyph->overlaps_vertically_p = false;
 31330       glyph->padding_p = false;
 31331       glyph->glyph_not_available_p = false;
 31332       glyph->face_id = it->face_id;
 31333       glyph->u.stretch.ascent = ascent;
 31334       glyph->u.stretch.height = height;
 31335       glyph->slice.img = null_glyph_slice;
 31336       glyph->font_type = FONT_TYPE_UNKNOWN;
 31337       if (it->bidi_p)
 31338         {
 31339           glyph->resolved_level = it->bidi_it.resolved_level;
 31340           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31341           glyph->bidi_type = it->bidi_it.type;
 31342         }
 31343       else
 31344         {
 31345           glyph->resolved_level = 0;
 31346           glyph->bidi_type = UNKNOWN_BT;
 31347         }
 31348       ++it->glyph_row->used[area];
 31349     }
 31350   else
 31351     IT_EXPAND_MATRIX_WIDTH (it, area);
 31352 }
 31353 
 31354 #endif  /* HAVE_WINDOW_SYSTEM */
 31355 
 31356 /* Produce a stretch glyph for iterator IT.  IT->object is the value
 31357    of the display property.  The value must be a list of the form
 31358    `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
 31359    being recognized:
 31360 
 31361    1. `:width WIDTH' specifies that the space should be WIDTH *
 31362    canonical char width wide.  WIDTH may be an integer or floating
 31363    point number.
 31364 
 31365    2. `:relative-width FACTOR' specifies that the width of the stretch
 31366    should be computed from the width of the first character having the
 31367    `display' property, and should be FACTOR times that width.
 31368 
 31369    3. `:align-to HPOS' specifies that the space should be wide enough
 31370    to reach HPOS, a value in canonical character units.
 31371 
 31372    Exactly one of the above pairs must be present.
 31373 
 31374    4. `:height HEIGHT' specifies that the height of the stretch produced
 31375    should be HEIGHT, measured in canonical character units.
 31376 
 31377    5. `:relative-height FACTOR' specifies that the height of the
 31378    stretch should be FACTOR times the height of the characters having
 31379    the display property.
 31380 
 31381    Either none or exactly one of 4 or 5 must be present.
 31382 
 31383    6. `:ascent ASCENT'  specifies that ASCENT percent of the height
 31384    of the stretch should be used for the ascent of the stretch.
 31385    ASCENT must be in the range 0 <= ASCENT <= 100.  */
 31386 
 31387 void
 31388 produce_stretch_glyph (struct it *it)
 31389 {
 31390   /* (space :width WIDTH :height HEIGHT ...)  */
 31391   Lisp_Object prop, plist;
 31392   int width = 0, height = 0, align_to = -1;
 31393   bool zero_width_ok_p = false;
 31394   double tem;
 31395   struct font *font = NULL;
 31396 
 31397 #ifdef HAVE_WINDOW_SYSTEM
 31398   int ascent = 0;
 31399   bool zero_height_ok_p = false;
 31400   struct face *face = NULL;     /* shut up GCC's -Wmaybe-uninitialized */
 31401 
 31402   if (FRAME_WINDOW_P (it->f))
 31403     {
 31404       face = FACE_FROM_ID (it->f, it->face_id);
 31405       font = face->font ? face->font : FRAME_FONT (it->f);
 31406       prepare_face_for_display (it->f, face);
 31407     }
 31408 #endif
 31409 
 31410   /* List should start with `space'.  */
 31411   eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
 31412   plist = XCDR (it->object);
 31413 
 31414   /* Compute the width of the stretch.  */
 31415   if ((prop = plist_get (plist, QCwidth), !NILP (prop))
 31416       && calc_pixel_width_or_height (&tem, it, prop, font, true, NULL))
 31417     {
 31418       /* Absolute width `:width WIDTH' specified and valid.  */
 31419       zero_width_ok_p = true;
 31420       width = (int)tem;
 31421     }
 31422   else if (prop = plist_get (plist, QCrelative_width), NUMVAL (prop) > 0)
 31423     {
 31424       /* Relative width `:relative-width FACTOR' specified and valid.
 31425          Compute the width of the characters having this `display'
 31426          property.  */
 31427       struct it it2;
 31428       Lisp_Object object =
 31429         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31430       unsigned char *p = (STRINGP (object)
 31431                           ? SDATA (object) + IT_STRING_BYTEPOS (*it)
 31432                           : BYTE_POS_ADDR (IT_BYTEPOS (*it)));
 31433       bool multibyte_p =
 31434         STRINGP (object) ? STRING_MULTIBYTE (object) : it->multibyte_p;
 31435 
 31436       it2 = *it;
 31437       if (multibyte_p)
 31438         {
 31439           it2.c = it2.char_to_display = string_char_and_length (p, &it2.len);
 31440 #ifdef HAVE_WINDOW_SYSTEM
 31441           if (FRAME_WINDOW_P (it->f) && ! ASCII_CHAR_P (it2.c))
 31442             it2.face_id = FACE_FOR_CHAR (it->f, face, it2.c,
 31443                                          IT_CHARPOS (*it),
 31444                                          STRINGP (object)? object : Qnil);
 31445 #endif
 31446         }
 31447       else
 31448         {
 31449           it2.c = it2.char_to_display = *p, it2.len = 1;
 31450           if (! ASCII_CHAR_P (it2.c))
 31451             it2.char_to_display = BYTE8_TO_CHAR (it2.c);
 31452         }
 31453 
 31454       it2.glyph_row = NULL;
 31455       it2.what = IT_CHARACTER;
 31456       PRODUCE_GLYPHS (&it2);
 31457       width = NUMVAL (prop) * it2.pixel_width;
 31458     }
 31459   else if ((prop = plist_get (plist, QCalign_to), !NILP (prop))
 31460            && calc_pixel_width_or_height (&tem, it, prop, font, true,
 31461                                           &align_to))
 31462     {
 31463       int x = it->current_x + it->continuation_lines_width;
 31464       int x0 = x;
 31465       /* Adjust for line numbers, if needed.   */
 31466       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31467         {
 31468           x -= it->lnum_pixel_width;
 31469           /* Restore the original width, if required.  */
 31470           if (x + it->stretch_adjust >= it->first_visible_x)
 31471             x += it->stretch_adjust;
 31472         }
 31473 
 31474       if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
 31475         align_to = (align_to < 0
 31476                     ? 0
 31477                     : align_to - window_box_left_offset (it->w, TEXT_AREA));
 31478       else if (align_to < 0)
 31479         align_to = window_box_left_offset (it->w, TEXT_AREA);
 31480       width = max (0, (int)tem + align_to - x);
 31481 
 31482       int next_x = x + width;
 31483       if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 31484         {
 31485           /* If the line is hscrolled, and the stretch starts before
 31486              the first visible pixel, simulate negative row->x.  */
 31487           if (x < it->first_visible_x)
 31488             {
 31489               next_x -= it->first_visible_x - x;
 31490               it->stretch_adjust = it->first_visible_x - x;
 31491             }
 31492           else
 31493             next_x -= it->stretch_adjust;
 31494         }
 31495       width = next_x - x0;
 31496       zero_width_ok_p = true;
 31497     }
 31498   else
 31499     /* Nothing specified -> width defaults to canonical char width.  */
 31500     width = FRAME_COLUMN_WIDTH (it->f);
 31501 
 31502   if (width <= 0 && (width < 0 || !zero_width_ok_p))
 31503     width = 1;
 31504 
 31505 #ifdef HAVE_WINDOW_SYSTEM
 31506   /* Compute height.  */
 31507   if (FRAME_WINDOW_P (it->f))
 31508     {
 31509       int default_height = normal_char_height (font, ' ');
 31510 
 31511       if ((prop = plist_get (plist, QCheight), !NILP (prop))
 31512           && calc_pixel_width_or_height (&tem, it, prop, font, false, NULL))
 31513         {
 31514           height = (int)tem;
 31515           zero_height_ok_p = true;
 31516         }
 31517       else if (prop = plist_get (plist, QCrelative_height),
 31518                NUMVAL (prop) > 0)
 31519         height = default_height * NUMVAL (prop);
 31520       else
 31521         height = default_height;
 31522 
 31523       if (height <= 0 && (height < 0 || !zero_height_ok_p))
 31524         height = 1;
 31525 
 31526       /* Compute percentage of height used for ascent.  If
 31527          `:ascent ASCENT' is present and valid, use that.  Otherwise,
 31528          derive the ascent from the font in use.  */
 31529       if (prop = plist_get (plist, QCascent),
 31530           NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
 31531         ascent = height * NUMVAL (prop) / 100.0;
 31532       else if (!NILP (prop)
 31533                && calc_pixel_width_or_height (&tem, it, prop, font, false, 0))
 31534         ascent = min (max (0, (int)tem), height);
 31535       else
 31536         ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
 31537     }
 31538   else
 31539 #endif  /* HAVE_WINDOW_SYSTEM */
 31540     height = 1;
 31541 
 31542   if (width > 0
 31543       && it->area == TEXT_AREA && it->line_wrap != TRUNCATE
 31544       && it->current_x + width > it->last_visible_x)
 31545     {
 31546       width = it->last_visible_x - it->current_x;
 31547 #ifdef HAVE_WINDOW_SYSTEM
 31548       /* Subtract one more pixel from the stretch width, but only on
 31549          GUI frames, since on a TTY each glyph is one "pixel" wide.  */
 31550       width -= FRAME_WINDOW_P (it->f);
 31551 #endif
 31552     }
 31553 
 31554   if (width > 0 && height > 0 && it->glyph_row)
 31555     {
 31556       Lisp_Object o_object = it->object;
 31557       Lisp_Object object =
 31558         it->sp > 0 ? it->stack[it->sp - 1].string : it->string;
 31559       int n = width;
 31560 
 31561       if (!STRINGP (object))
 31562         object = it->w->contents;
 31563 #ifdef HAVE_WINDOW_SYSTEM
 31564       if (FRAME_WINDOW_P (it->f))
 31565         append_stretch_glyph (it, object, width, height, ascent);
 31566       else
 31567 #endif
 31568         {
 31569           it->object = object;
 31570           it->char_to_display = ' ';
 31571           it->pixel_width = it->len = 1;
 31572           while (n--)
 31573             tty_append_glyph (it);
 31574           it->object = o_object;
 31575         }
 31576     }
 31577 
 31578   it->pixel_width = width;
 31579 #ifdef HAVE_WINDOW_SYSTEM
 31580   if (FRAME_WINDOW_P (it->f))
 31581     {
 31582       it->ascent = it->phys_ascent = ascent;
 31583       it->descent = it->phys_descent = height - it->ascent;
 31584       it->nglyphs = width > 0 && height > 0;
 31585       take_vertical_position_into_account (it);
 31586     }
 31587   else
 31588 #endif
 31589     it->nglyphs = width;
 31590 }
 31591 
 31592 /* Get information about special display element WHAT in an
 31593    environment described by IT.  WHAT is one of IT_TRUNCATION or
 31594    IT_CONTINUATION.  Maybe produce glyphs for WHAT if IT has a
 31595    non-null glyph_row member.  This function ensures that fields like
 31596    face_id, c, len of IT are left untouched.  */
 31597 
 31598 static void
 31599 produce_special_glyphs (struct it *it, enum display_element_type what)
 31600 {
 31601   struct it temp_it;
 31602   Lisp_Object gc;
 31603   GLYPH glyph;
 31604 
 31605   temp_it = *it;
 31606   temp_it.object = Qnil;
 31607   memset (&temp_it.current, 0, sizeof temp_it.current);
 31608 
 31609   if (what == IT_CONTINUATION)
 31610     {
 31611       /* Continuation glyph.  For R2L lines, we mirror it by hand.  */
 31612       if (it->bidi_it.paragraph_dir == R2L)
 31613         SET_GLYPH_FROM_CHAR (glyph, '/');
 31614       else
 31615         SET_GLYPH_FROM_CHAR (glyph, '\\');
 31616       if (it->dp
 31617           && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31618         {
 31619           /* FIXME: Should we mirror GC for R2L lines?  */
 31620           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31621           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31622         }
 31623     }
 31624   else if (what == IT_TRUNCATION)
 31625     {
 31626       /* Truncation glyph.  */
 31627       SET_GLYPH_FROM_CHAR (glyph, '$');
 31628       if (it->dp
 31629           && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
 31630         {
 31631           /* FIXME: Should we mirror GC for R2L lines?  */
 31632           SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
 31633           spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
 31634         }
 31635     }
 31636   else
 31637     emacs_abort ();
 31638 
 31639 #ifdef HAVE_WINDOW_SYSTEM
 31640   /* On a GUI frame, when the right fringe (left fringe for R2L rows)
 31641      is turned off, we precede the truncation/continuation glyphs by a
 31642      stretch glyph whose width is computed such that these special
 31643      glyphs are aligned at the window margin, even when very different
 31644      fonts are used in different glyph rows.  */
 31645   if (FRAME_WINDOW_P (temp_it.f)
 31646       /* init_iterator calls this with it->glyph_row == NULL, and it
 31647          wants only the pixel width of the truncation/continuation
 31648          glyphs.  */
 31649       && temp_it.glyph_row
 31650       /* insert_left_trunc_glyphs calls us at the beginning of the
 31651          row, and it has its own calculation of the stretch glyph
 31652          width.  */
 31653       && temp_it.glyph_row->used[TEXT_AREA] > 0
 31654       && (temp_it.glyph_row->reversed_p
 31655           ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
 31656           : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
 31657     {
 31658       int stretch_width = temp_it.last_visible_x - temp_it.current_x;
 31659 
 31660       if (stretch_width > 0)
 31661         {
 31662           struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
 31663           struct font *font =
 31664             face->font ? face->font : FRAME_FONT (temp_it.f);
 31665           int stretch_ascent =
 31666             (((temp_it.ascent + temp_it.descent)
 31667               * FONT_BASE (font)) / FONT_HEIGHT (font));
 31668 
 31669           append_stretch_glyph (&temp_it, Qnil, stretch_width,
 31670                                 temp_it.ascent + temp_it.descent,
 31671                                 stretch_ascent);
 31672         }
 31673     }
 31674 #endif
 31675 
 31676   temp_it.dp = NULL;
 31677   temp_it.what = IT_CHARACTER;
 31678   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31679   temp_it.face_id = GLYPH_FACE (glyph);
 31680   temp_it.len = CHAR_BYTES (temp_it.c);
 31681 
 31682   PRODUCE_GLYPHS (&temp_it);
 31683   it->pixel_width = temp_it.pixel_width;
 31684   it->nglyphs = temp_it.nglyphs;
 31685 }
 31686 
 31687 /* Produce padding glyphs for mode/header/tab-line whose text needs to
 31688    be truncated.  This is used when the last visible character leaves
 31689    one or more columns till the window edge, but the next character is
 31690    wider than that number of columns, and therefore cannot fit on the
 31691    line.  We then replace these columns with the appropriate padding
 31692    character: '-' for the mode line and SPC for the other two.  That's
 31693    because these lines should not show the usual truncation glyphs
 31694    there.  This function is only used on TTY frames.  */
 31695 static void
 31696 pad_mode_line (struct it *it, bool mode_line_p)
 31697 {
 31698   struct it temp_it;
 31699   GLYPH glyph;
 31700 
 31701   eassert (!FRAME_WINDOW_P (it->f));
 31702   temp_it = *it;
 31703   temp_it.object = Qnil;
 31704   memset (&temp_it.current, 0, sizeof temp_it.current);
 31705 
 31706   SET_GLYPH (glyph, mode_line_p ? '-' : ' ', it->base_face_id);
 31707 
 31708   temp_it.dp = NULL;
 31709   temp_it.what = IT_CHARACTER;
 31710   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
 31711   temp_it.face_id = GLYPH_FACE (glyph);
 31712   temp_it.len = CHAR_BYTES (temp_it.c);
 31713 
 31714   PRODUCE_GLYPHS (&temp_it);
 31715   it->pixel_width = temp_it.pixel_width;
 31716   it->nglyphs = temp_it.nglyphs;
 31717 }
 31718 
 31719 #ifdef HAVE_WINDOW_SYSTEM
 31720 
 31721 /* Calculate line-height and line-spacing properties.
 31722    An integer value specifies explicit pixel value.
 31723    A float value specifies relative value to current face height.
 31724    A cons (float . face-name) specifies relative value to
 31725    height of specified face font.
 31726 
 31727    Returns height in pixels, or nil.  */
 31728 
 31729 static Lisp_Object
 31730 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
 31731                            int boff, bool override)
 31732 {
 31733   Lisp_Object face_name = Qnil;
 31734   int ascent, descent, height;
 31735 
 31736   if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
 31737     return val;
 31738 
 31739   if (CONSP (val))
 31740     {
 31741       face_name = XCAR (val);
 31742       val = XCDR (val);
 31743       if (!NUMBERP (val))
 31744         val = make_fixnum (1);
 31745       if (NILP (face_name))
 31746         {
 31747           height = it->ascent + it->descent;
 31748           goto scale;
 31749         }
 31750     }
 31751 
 31752   if (NILP (face_name))
 31753     {
 31754       font = FRAME_FONT (it->f);
 31755       boff = FRAME_BASELINE_OFFSET (it->f);
 31756     }
 31757   else if (EQ (face_name, Qt))
 31758     {
 31759       override = false;
 31760     }
 31761   else
 31762     {
 31763       int face_id;
 31764       struct face *face;
 31765 
 31766       face_id = lookup_named_face (it->w, it->f, face_name, false);
 31767       face = FACE_FROM_ID_OR_NULL (it->f, face_id);
 31768       if (face == NULL || ((font = face->font) == NULL))
 31769         return make_fixnum (-1);
 31770       boff = font->baseline_offset;
 31771       if (font->vertical_centering)
 31772         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 31773     }
 31774 
 31775   normal_char_ascent_descent (font, -1, &ascent, &descent);
 31776 
 31777   if (override)
 31778     {
 31779       it->override_ascent = ascent;
 31780       it->override_descent = descent;
 31781       it->override_boff = boff;
 31782     }
 31783 
 31784   height = ascent + descent;
 31785 
 31786  scale:
 31787   /* FIXME: Check for overflow in multiplication or conversion.  */
 31788   if (FLOATP (val))
 31789     height = (int)(XFLOAT_DATA (val) * height);
 31790   else if (INTEGERP (val))
 31791     {
 31792       intmax_t v;
 31793       if (integer_to_intmax (val, &v))
 31794         height *= v;
 31795     }
 31796 
 31797   return make_fixnum (height);
 31798 }
 31799 
 31800 
 31801 /* Append a glyph for a glyphless character to IT->glyph_row.  FACE_ID
 31802    is a face ID to be used for the glyph.  FOR_NO_FONT is true if
 31803    and only if this is for a character for which no font was found.
 31804 
 31805    If the display method (it->glyphless_method) is
 31806    GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
 31807    length of the acronym or the hexadecimal string, UPPER_XOFF and
 31808    UPPER_YOFF are pixel offsets for the upper part of the string,
 31809    LOWER_XOFF and LOWER_YOFF are for the lower part.
 31810 
 31811    For the other display methods, LEN through LOWER_YOFF are zero.  */
 31812 
 31813 static void
 31814 append_glyphless_glyph (struct it *it, int face_id, bool for_no_font, int len,
 31815                         short upper_xoff, short upper_yoff,
 31816                         short lower_xoff, short lower_yoff)
 31817 {
 31818   struct glyph *glyph;
 31819   enum glyph_row_area area = it->area;
 31820 
 31821   glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
 31822   if (glyph < it->glyph_row->glyphs[area + 1])
 31823     {
 31824       /* If the glyph row is reversed, we need to prepend the glyph
 31825          rather than append it.  */
 31826       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31827         {
 31828           struct glyph *g;
 31829 
 31830           /* Make room for the additional glyph.  */
 31831           for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
 31832             g[1] = *g;
 31833           glyph = it->glyph_row->glyphs[area];
 31834         }
 31835       glyph->charpos = CHARPOS (it->position);
 31836       glyph->object = it->object;
 31837       eassert (it->pixel_width <= SHRT_MAX);
 31838       glyph->pixel_width = it->pixel_width;
 31839       glyph->ascent = it->ascent;
 31840       glyph->descent = it->descent;
 31841       glyph->voffset = it->voffset;
 31842       glyph->type = GLYPHLESS_GLYPH;
 31843       glyph->u.glyphless.method = it->glyphless_method;
 31844       glyph->u.glyphless.for_no_font = for_no_font;
 31845       glyph->u.glyphless.len = len;
 31846       glyph->u.glyphless.ch = it->c;
 31847       glyph->slice.glyphless.upper_xoff = upper_xoff;
 31848       glyph->slice.glyphless.upper_yoff = upper_yoff;
 31849       glyph->slice.glyphless.lower_xoff = lower_xoff;
 31850       glyph->slice.glyphless.lower_yoff = lower_yoff;
 31851       glyph->avoid_cursor_p = it->avoid_cursor_p;
 31852       glyph->multibyte_p = it->multibyte_p;
 31853       if (it->glyph_row->reversed_p && area == TEXT_AREA)
 31854         {
 31855           /* In R2L rows, the left and the right box edges need to be
 31856              drawn in reverse direction.  */
 31857           glyph->right_box_line_p = it->start_of_box_run_p;
 31858           glyph->left_box_line_p = it->end_of_box_run_p;
 31859         }
 31860       else
 31861         {
 31862           glyph->left_box_line_p = it->start_of_box_run_p;
 31863           glyph->right_box_line_p = it->end_of_box_run_p;
 31864         }
 31865       glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
 31866                                       || it->phys_descent > it->descent);
 31867       glyph->padding_p = false;
 31868       glyph->glyph_not_available_p = false;
 31869       glyph->face_id = face_id;
 31870       glyph->font_type = FONT_TYPE_UNKNOWN;
 31871       if (it->bidi_p)
 31872         {
 31873           glyph->resolved_level = it->bidi_it.resolved_level;
 31874           eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
 31875           glyph->bidi_type = it->bidi_it.type;
 31876         }
 31877       ++it->glyph_row->used[area];
 31878     }
 31879   else
 31880     IT_EXPAND_MATRIX_WIDTH (it, area);
 31881 }
 31882 
 31883 
 31884 /* Produce a glyph for a glyphless character for iterator IT.
 31885    IT->glyphless_method specifies which method to use for displaying
 31886    the character.  See the description of enum
 31887    glyphless_display_method in dispextern.h for the detail.
 31888 
 31889    FOR_NO_FONT is true if and only if this is for a character for
 31890    which no font was found.  ACRONYM, if non-nil, is an acronym string
 31891    for the character.  */
 31892 
 31893 static void
 31894 produce_glyphless_glyph (struct it *it, bool for_no_font, Lisp_Object acronym)
 31895 {
 31896   int face_id;
 31897   struct face *face;
 31898   struct font *font;
 31899   int base_width, base_height, width, height;
 31900   short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
 31901   int len;
 31902 
 31903   /* Get the metrics of the base font.  We always refer to the current
 31904      ASCII face.  */
 31905   face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
 31906   font = face->font ? face->font : FRAME_FONT (it->f);
 31907   normal_char_ascent_descent (font, -1, &it->ascent, &it->descent);
 31908   it->ascent += font->baseline_offset;
 31909   it->descent -= font->baseline_offset;
 31910   base_height = it->ascent + it->descent;
 31911   base_width = font->average_width;
 31912 
 31913   face_id = merge_glyphless_glyph_face (it);
 31914 
 31915   if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
 31916     {
 31917       it->pixel_width = THIN_SPACE_WIDTH;
 31918       len = 0;
 31919       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31920     }
 31921   else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
 31922     {
 31923       width = CHARACTER_WIDTH (it->c);
 31924       if (width == 0)
 31925         width = 1;
 31926       else if (width > 4)
 31927         width = 4;
 31928       it->pixel_width = base_width * width;
 31929       len = 0;
 31930       upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
 31931     }
 31932   else
 31933     {
 31934       char buf[7];
 31935       const char *str;
 31936       unsigned int code[6];
 31937       int upper_len;
 31938       int ascent, descent;
 31939       struct font_metrics metrics_upper, metrics_lower;
 31940 
 31941       face = FACE_FROM_ID (it->f, face_id);
 31942       font = face->font ? face->font : FRAME_FONT (it->f);
 31943       prepare_face_for_display (it->f, face);
 31944 
 31945       if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
 31946         {
 31947           if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
 31948             acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
 31949           if (CONSP (acronym))
 31950             acronym = XCAR (acronym);
 31951           str = STRINGP (acronym) ? SSDATA (acronym) : "";
 31952         }
 31953       else
 31954         {
 31955           eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
 31956           sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c + 0u);
 31957           str = buf;
 31958         }
 31959       for (len = 0; str[len] && ASCII_CHAR_P (str[len]) && len < 6; len++)
 31960         code[len] = font->driver->encode_char (font, str[len]);
 31961       upper_len = (len + 1) / 2;
 31962       font->driver->text_extents (font, code, upper_len,
 31963                                   &metrics_upper);
 31964       font->driver->text_extents (font, code + upper_len, len - upper_len,
 31965                                   &metrics_lower);
 31966 
 31967 
 31968 
 31969       /* +4 is for vertical bars of a box plus 1-pixel spaces at both side.  */
 31970       width = max (metrics_upper.width, metrics_lower.width) + 4;
 31971       upper_xoff = lower_xoff = 2; /* the typical case */
 31972       if (base_width >= width)
 31973         {
 31974           /* Align the upper to the left, the lower to the right.  */
 31975           it->pixel_width = base_width;
 31976           lower_xoff = base_width - 2 - metrics_lower.width;
 31977         }
 31978       else
 31979         {
 31980           /* Center the shorter one.  */
 31981           it->pixel_width = width;
 31982           if (metrics_upper.width >= metrics_lower.width)
 31983             lower_xoff = (width - metrics_lower.width) / 2;
 31984           else
 31985             upper_xoff = (width - metrics_upper.width) / 2;
 31986         }
 31987 
 31988       /* +5 is for horizontal bars of a box plus 1-pixel spaces at
 31989          top, bottom, and between upper and lower strings.  */
 31990       height = (metrics_upper.ascent + metrics_upper.descent
 31991                 + metrics_lower.ascent + metrics_lower.descent) + 5;
 31992       /* Center vertically.
 31993          H:base_height, D:base_descent
 31994          h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
 31995 
 31996          ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
 31997          descent = D - H/2 + h/2;
 31998          lower_yoff = descent - 2 - ld;
 31999          upper_yoff = lower_yoff - la - 1 - ud;  */
 32000       ascent = - (it->descent - (base_height + height + 1) / 2);
 32001       descent = it->descent - (base_height - height) / 2;
 32002       lower_yoff = descent - 2 - metrics_lower.descent;
 32003       upper_yoff = (lower_yoff - metrics_lower.ascent - 1
 32004                     - metrics_upper.descent);
 32005       /* Don't make the height shorter than the base height.  */
 32006       if (height > base_height)
 32007         {
 32008           it->ascent = ascent;
 32009           it->descent = descent;
 32010         }
 32011     }
 32012 
 32013   it->phys_ascent = it->ascent;
 32014   it->phys_descent = it->descent;
 32015   if (it->glyph_row)
 32016     append_glyphless_glyph (it, face_id, for_no_font, len,
 32017                             upper_xoff, upper_yoff,
 32018                             lower_xoff, lower_yoff);
 32019   it->nglyphs = 1;
 32020   take_vertical_position_into_account (it);
 32021 }
 32022 
 32023 
 32024 /* If face has a box, add the box thickness to the character
 32025    height.  If character has a box line to the left and/or
 32026    right, add the box line width to the character's width.  */
 32027 #define IT_APPLY_FACE_BOX(it, face)                             \
 32028   do {                                                          \
 32029     if (face->box != FACE_NO_BOX)                               \
 32030       {                                                         \
 32031         int thick = face->box_horizontal_line_width;            \
 32032         if (thick > 0)                                          \
 32033           {                                                     \
 32034             it->ascent += thick;                                \
 32035             it->descent += thick;                               \
 32036           }                                                     \
 32037                                                                 \
 32038         thick = face->box_vertical_line_width;                  \
 32039         if (thick > 0)                                          \
 32040           {                                                     \
 32041             if (it->start_of_box_run_p)                         \
 32042               it->pixel_width += thick;                         \
 32043             if (it->end_of_box_run_p)                           \
 32044               it->pixel_width += thick;                         \
 32045           }                                                     \
 32046       }                                                         \
 32047     } while (false)
 32048 
 32049 /* RIF:
 32050    Produce glyphs/get display metrics for the display element IT is
 32051    loaded with.  See the description of struct it in dispextern.h
 32052    for an overview of struct it.  */
 32053 
 32054 void
 32055 gui_produce_glyphs (struct it *it)
 32056 {
 32057   int extra_line_spacing = it->extra_line_spacing;
 32058 
 32059   it->glyph_not_available_p = false;
 32060 
 32061   if (it->what == IT_CHARACTER)
 32062     {
 32063       unsigned char2b;
 32064       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32065       struct font *font = face->font;
 32066       struct font_metrics *pcm = NULL;
 32067       int boff;                 /* Baseline offset.  */
 32068 
 32069       if (font == NULL)
 32070         {
 32071           /* When no suitable font is found, display this character by
 32072              the method specified in the first extra slot of
 32073              Vglyphless_char_display.  */
 32074           Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
 32075 
 32076           eassert (it->what == IT_GLYPHLESS);
 32077           produce_glyphless_glyph (it, true,
 32078                                    STRINGP (acronym) ? acronym : Qnil);
 32079           goto done;
 32080         }
 32081 
 32082       boff = font->baseline_offset;
 32083       if (font->vertical_centering)
 32084         boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32085 
 32086       if (it->char_to_display != '\n' && it->char_to_display != '\t')
 32087         {
 32088           it->nglyphs = 1;
 32089 
 32090           if (it->override_ascent >= 0)
 32091             {
 32092               it->ascent = it->override_ascent;
 32093               it->descent = it->override_descent;
 32094               boff = it->override_boff;
 32095             }
 32096           else
 32097             {
 32098               it->ascent = FONT_BASE (font) + boff;
 32099               it->descent = FONT_DESCENT (font) - boff;
 32100             }
 32101 
 32102           if (get_char_glyph_code (it->char_to_display, font, &char2b))
 32103             {
 32104               pcm = get_per_char_metric (font, &char2b);
 32105               if (pcm->width == 0
 32106                   && pcm->rbearing == 0 && pcm->lbearing == 0)
 32107                 pcm = NULL;
 32108             }
 32109 
 32110           if (pcm)
 32111             {
 32112               it->phys_ascent = pcm->ascent + boff;
 32113               it->phys_descent = pcm->descent - boff;
 32114               it->pixel_width = pcm->width;
 32115               /* Don't use font-global values for ascent and descent
 32116                  if they result in an exceedingly large line height.  */
 32117               if (it->override_ascent < 0)
 32118                 {
 32119                   if (FONT_TOO_HIGH (font))
 32120                     {
 32121                       it->ascent = it->phys_ascent;
 32122                       it->descent = it->phys_descent;
 32123                       /* These limitations are enforced by an
 32124                          assertion near the end of this function.  */
 32125                       if (it->ascent < 0)
 32126                         it->ascent = 0;
 32127                       if (it->descent < 0)
 32128                         it->descent = 0;
 32129                     }
 32130                 }
 32131             }
 32132           else
 32133             {
 32134               it->glyph_not_available_p = true;
 32135               it->phys_ascent = it->ascent;
 32136               it->phys_descent = it->descent;
 32137               it->pixel_width = font->space_width;
 32138             }
 32139 
 32140           if (it->constrain_row_ascent_descent_p)
 32141             {
 32142               if (it->descent > it->max_descent)
 32143                 {
 32144                   it->ascent += it->descent - it->max_descent;
 32145                   it->descent = it->max_descent;
 32146                 }
 32147               if (it->ascent > it->max_ascent)
 32148                 {
 32149                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32150                   it->ascent = it->max_ascent;
 32151                 }
 32152               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32153               it->phys_descent = min (it->phys_descent, it->descent);
 32154               extra_line_spacing = 0;
 32155             }
 32156 
 32157           /* If this is a space inside a region of text with
 32158              `space-width' property, change its width.  */
 32159           bool stretched_p
 32160             = it->char_to_display == ' ' && !NILP (it->space_width);
 32161           if (stretched_p)
 32162             it->pixel_width *= XFLOATINT (it->space_width);
 32163 
 32164           IT_APPLY_FACE_BOX(it, face);
 32165 
 32166           /* If face has an overline, add the height of the overline
 32167              (1 pixel) and a 1 pixel margin to the character height.  */
 32168           if (face->overline_p)
 32169             it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32170 
 32171           if (it->constrain_row_ascent_descent_p)
 32172             {
 32173               if (it->ascent > it->max_ascent)
 32174                 it->ascent = it->max_ascent;
 32175               if (it->descent > it->max_descent)
 32176                 it->descent = it->max_descent;
 32177             }
 32178 
 32179           take_vertical_position_into_account (it);
 32180 
 32181           /* If we have to actually produce glyphs, do it.  */
 32182           if (it->glyph_row)
 32183             {
 32184               if (stretched_p)
 32185                 {
 32186                   /* Translate a space with a `space-width' property
 32187                      into a stretch glyph.  */
 32188                   int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
 32189                                 / FONT_HEIGHT (font));
 32190                   append_stretch_glyph (it, it->object, it->pixel_width,
 32191                                         it->ascent + it->descent, ascent);
 32192                 }
 32193               else
 32194                 append_glyph (it);
 32195 
 32196               /* If characters with lbearing or rbearing are displayed
 32197                  in this line, record that fact in a flag of the
 32198                  glyph row.  This is used to optimize X output code.  */
 32199               if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
 32200                 it->glyph_row->contains_overlapping_glyphs_p = true;
 32201             }
 32202           if (! stretched_p && it->pixel_width == 0)
 32203             /* We assure that all visible glyphs have at least 1-pixel
 32204                width.  */
 32205             it->pixel_width = 1;
 32206         }
 32207       else if (it->char_to_display == '\n')
 32208         {
 32209           /* A newline has no width, but we need the height of the
 32210              line.  But if previous part of the line sets a height,
 32211              don't increase that height.  */
 32212 
 32213           Lisp_Object height;
 32214           Lisp_Object total_height = Qnil;
 32215 
 32216           it->override_ascent = -1;
 32217           it->pixel_width = 0;
 32218           it->nglyphs = 0;
 32219 
 32220           height = get_it_property (it, Qline_height);
 32221           /* Split (line-height total-height) list.  */
 32222           if (CONSP (height)
 32223               && CONSP (XCDR (height))
 32224               && NILP (XCDR (XCDR (height))))
 32225             {
 32226               total_height = XCAR (XCDR (height));
 32227               height = XCAR (height);
 32228             }
 32229           height = calc_line_height_property (it, height, font, boff, true);
 32230 
 32231           if (it->override_ascent >= 0)
 32232             {
 32233               it->ascent = it->override_ascent;
 32234               it->descent = it->override_descent;
 32235               boff = it->override_boff;
 32236             }
 32237           else
 32238             {
 32239               if (FONT_TOO_HIGH (font))
 32240                 {
 32241                   it->ascent = font->pixel_size + boff - 1;
 32242                   it->descent = -boff + 1;
 32243                   if (it->descent < 0)
 32244                     it->descent = 0;
 32245                 }
 32246               else
 32247                 {
 32248                   it->ascent = FONT_BASE (font) + boff;
 32249                   it->descent = FONT_DESCENT (font) - boff;
 32250                 }
 32251             }
 32252 
 32253           if (EQ (height, Qt))
 32254             {
 32255               if (it->descent > it->max_descent)
 32256                 {
 32257                   it->ascent += it->descent - it->max_descent;
 32258                   it->descent = it->max_descent;
 32259                 }
 32260               if (it->ascent > it->max_ascent)
 32261                 {
 32262                   it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
 32263                   it->ascent = it->max_ascent;
 32264                 }
 32265               it->phys_ascent = min (it->phys_ascent, it->ascent);
 32266               it->phys_descent = min (it->phys_descent, it->descent);
 32267               it->constrain_row_ascent_descent_p = true;
 32268               extra_line_spacing = 0;
 32269             }
 32270           else
 32271             {
 32272               Lisp_Object spacing;
 32273 
 32274               it->phys_ascent = it->ascent;
 32275               it->phys_descent = it->descent;
 32276 
 32277               if ((it->max_ascent > 0 || it->max_descent > 0)
 32278                   && face->box != FACE_NO_BOX
 32279                   && face->box_horizontal_line_width > 0)
 32280                 {
 32281                   it->ascent += face->box_horizontal_line_width;
 32282                   it->descent += face->box_horizontal_line_width;
 32283                 }
 32284               if (!NILP (height)
 32285                   && XFIXNUM (height) > it->ascent + it->descent)
 32286                 it->ascent = XFIXNUM (height) - it->descent;
 32287 
 32288               if (!NILP (total_height))
 32289                 spacing = calc_line_height_property (it, total_height, font,
 32290                                                      boff, false);
 32291               else
 32292                 {
 32293                   spacing = get_it_property (it, Qline_spacing);
 32294                   spacing = calc_line_height_property (it, spacing, font,
 32295                                                        boff, false);
 32296                 }
 32297               if (FIXNUMP (spacing))
 32298                 {
 32299                   extra_line_spacing = XFIXNUM (spacing);
 32300                   if (!NILP (total_height))
 32301                     extra_line_spacing -= (it->phys_ascent + it->phys_descent);
 32302                 }
 32303             }
 32304         }
 32305       else                    /* i.e. (it->char_to_display == '\t') */
 32306         {
 32307           if (font->space_width > 0)
 32308             {
 32309               int tab_width = it->tab_width * font->space_width;
 32310               int x = it->current_x + it->continuation_lines_width;
 32311               int x0 = x;
 32312               /* Adjust for line numbers, if needed.   */
 32313               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32314                 {
 32315                   x -= it->lnum_pixel_width;
 32316                   /* Restore the original TAB width, if required.  */
 32317                   if (x + it->stretch_adjust >= it->first_visible_x)
 32318                     x += it->stretch_adjust;
 32319                 }
 32320 
 32321               int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
 32322 
 32323               /* If the distance from the current position to the next tab
 32324                  stop is less than a space character width, use the
 32325                  tab stop after that.  */
 32326               if (next_tab_x - x < font->space_width)
 32327                 next_tab_x += tab_width;
 32328               if (!NILP (Vdisplay_line_numbers) && it->line_number_produced_p)
 32329                 {
 32330                   next_tab_x += it->lnum_pixel_width;
 32331                   /* If the line is hscrolled, and the TAB starts before
 32332                      the first visible pixel, simulate negative row->x.  */
 32333                   if (x < it->first_visible_x)
 32334                     {
 32335                       next_tab_x -= it->first_visible_x - x;
 32336                       it->stretch_adjust = it->first_visible_x - x;
 32337                     }
 32338                   else
 32339                     next_tab_x -= it->stretch_adjust;
 32340                 }
 32341 
 32342               it->pixel_width = next_tab_x - x0;
 32343               it->nglyphs = 1;
 32344               if (FONT_TOO_HIGH (font))
 32345                 {
 32346                   if (get_char_glyph_code (' ', font, &char2b))
 32347                     {
 32348                       pcm = get_per_char_metric (font, &char2b);
 32349                       if (pcm->width == 0
 32350                           && pcm->rbearing == 0 && pcm->lbearing == 0)
 32351                         pcm = NULL;
 32352                     }
 32353 
 32354                   if (pcm)
 32355                     {
 32356                       it->ascent = pcm->ascent + boff;
 32357                       it->descent = pcm->descent - boff;
 32358                     }
 32359                   else
 32360                     {
 32361                       it->ascent = font->pixel_size + boff - 1;
 32362                       it->descent = -boff + 1;
 32363                     }
 32364                   if (it->ascent < 0)
 32365                     it->ascent = 0;
 32366                   if (it->descent < 0)
 32367                     it->descent = 0;
 32368                 }
 32369               else
 32370                 {
 32371                   it->ascent = FONT_BASE (font) + boff;
 32372                   it->descent = FONT_DESCENT (font) - boff;
 32373                 }
 32374               it->phys_ascent = it->ascent;
 32375               it->phys_descent = it->descent;
 32376 
 32377               if (it->glyph_row)
 32378                 {
 32379                   append_stretch_glyph (it, it->object, it->pixel_width,
 32380                                         it->ascent + it->descent, it->ascent);
 32381                 }
 32382             }
 32383           else
 32384             {
 32385               it->pixel_width = 0;
 32386               it->nglyphs = 1;
 32387             }
 32388         }
 32389 
 32390       if (FONT_TOO_HIGH (font))
 32391         {
 32392           int font_ascent, font_descent;
 32393 
 32394           /* For very large fonts, where we ignore the declared font
 32395              dimensions, and go by per-character metrics instead,
 32396              don't let the row ascent and descent values (and the row
 32397              height computed from them) be smaller than the "normal"
 32398              character metrics.  This avoids unpleasant effects
 32399              whereby lines on display would change their height
 32400              depending on which characters are shown.  */
 32401           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32402           it->max_ascent = max (it->max_ascent, font_ascent);
 32403           it->max_descent = max (it->max_descent, font_descent);
 32404         }
 32405 
 32406       if (it->ascent < 0)
 32407         it->ascent = 0;
 32408       if (it->descent < 0)
 32409         it->descent = 0;
 32410     }
 32411   else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
 32412     {
 32413       /* A static composition.
 32414 
 32415          Note: A composition is represented as one glyph in the
 32416          glyph matrix.  There are no padding glyphs.
 32417 
 32418          Important note: pixel_width, ascent, and descent are the
 32419          values of what is drawn by draw_glyphs (i.e. the values of
 32420          the overall glyphs composed).  */
 32421       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32422       int boff;                 /* baseline offset */
 32423       struct composition *cmp = composition_table[it->cmp_it.id];
 32424       int glyph_len = cmp->glyph_len;
 32425       struct font *font = face->font;
 32426 
 32427       it->nglyphs = 1;
 32428 
 32429       /* If we have not yet calculated pixel size data of glyphs of
 32430          the composition for the current face font, calculate them
 32431          now.  Theoretically, we have to check all fonts for the
 32432          glyphs, but that requires much time and memory space.  So,
 32433          here we check only the font of the first glyph.  This may
 32434          lead to incorrect display, but it's very rare, and C-l
 32435          (recenter-top-bottom) can correct the display anyway.  */
 32436       if (! cmp->font || cmp->font != font)
 32437         {
 32438           /* Ascent and descent of the font of the first character
 32439              of this composition (adjusted by baseline offset).
 32440              Ascent and descent of overall glyphs should not be less
 32441              than these, respectively.  */
 32442           int font_ascent, font_descent, font_height;
 32443           /* Bounding box of the overall glyphs.  */
 32444           int leftmost, rightmost, lowest, highest;
 32445           int lbearing, rbearing;
 32446           int i, width, ascent, descent;
 32447           int c;
 32448           unsigned char2b;
 32449           struct font_metrics *pcm;
 32450           ptrdiff_t pos;
 32451 
 32452           eassume (0 < glyph_len); /* See Bug#8512.  */
 32453           do
 32454             c = COMPOSITION_GLYPH (cmp, glyph_len - 1);
 32455           while (c == '\t' && 0 < --glyph_len);
 32456 
 32457           bool right_padded = glyph_len < cmp->glyph_len;
 32458           for (i = 0; i < glyph_len; i++)
 32459             {
 32460               c = COMPOSITION_GLYPH (cmp, i);
 32461               if (c != '\t')
 32462                 break;
 32463               cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32464             }
 32465           bool left_padded = i > 0;
 32466 
 32467           pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
 32468                  : IT_CHARPOS (*it));
 32469           /* If no suitable font is found, use the default font.  */
 32470           bool font_not_found_p = font == NULL;
 32471           if (font_not_found_p)
 32472             {
 32473               face = face->ascii_face;
 32474               font = face->font;
 32475             }
 32476           boff = font->baseline_offset;
 32477           if (font->vertical_centering)
 32478             boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
 32479           normal_char_ascent_descent (font, -1, &font_ascent, &font_descent);
 32480           font_ascent +=  boff;
 32481           font_descent -= boff;
 32482           font_height = font_ascent + font_descent;
 32483 
 32484           cmp->font = font;
 32485 
 32486           pcm = NULL;
 32487           if (! font_not_found_p)
 32488             {
 32489               get_char_face_and_encoding (it->f, c, it->face_id,
 32490                                           &char2b, false);
 32491               pcm = get_per_char_metric (font, &char2b);
 32492             }
 32493 
 32494           /* Initialize the bounding box.  */
 32495           if (pcm)
 32496             {
 32497               width = cmp->glyph_len > 0 ? pcm->width : 0;
 32498               ascent = pcm->ascent;
 32499               descent = pcm->descent;
 32500               lbearing = pcm->lbearing;
 32501               rbearing = pcm->rbearing;
 32502             }
 32503           else
 32504             {
 32505               width = cmp->glyph_len > 0 ? font->space_width : 0;
 32506               ascent = FONT_BASE (font);
 32507               descent = FONT_DESCENT (font);
 32508               lbearing = 0;
 32509               rbearing = width;
 32510             }
 32511 
 32512           rightmost = width;
 32513           leftmost = 0;
 32514           lowest = - descent + boff;
 32515           highest = ascent + boff;
 32516 
 32517           if (! font_not_found_p
 32518               && font->default_ascent
 32519               && CHAR_TABLE_P (Vuse_default_ascent)
 32520               && !NILP (Faref (Vuse_default_ascent,
 32521                                make_fixnum (it->char_to_display))))
 32522             highest = font->default_ascent + boff;
 32523 
 32524           /* Draw the first glyph at the normal position.  It may be
 32525              shifted to right later if some other glyphs are drawn
 32526              at the left.  */
 32527           cmp->offsets[i * 2] = 0;
 32528           cmp->offsets[i * 2 + 1] = boff;
 32529           cmp->lbearing = lbearing;
 32530           cmp->rbearing = rbearing;
 32531 
 32532           /* Set cmp->offsets for the remaining glyphs.  */
 32533           for (i++; i < glyph_len; i++)
 32534             {
 32535               int left, right, btm, top;
 32536               int ch = COMPOSITION_GLYPH (cmp, i);
 32537               int face_id;
 32538               struct face *this_face;
 32539 
 32540               if (ch == '\t')
 32541                 ch = ' ';
 32542               face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
 32543               this_face = FACE_FROM_ID (it->f, face_id);
 32544               font = this_face->font;
 32545 
 32546               if (font == NULL)
 32547                 pcm = NULL;
 32548               else
 32549                 {
 32550                   get_char_face_and_encoding (it->f, ch, face_id,
 32551                                               &char2b, false);
 32552                   pcm = get_per_char_metric (font, &char2b);
 32553                 }
 32554               if (! pcm)
 32555                 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
 32556               else
 32557                 {
 32558                   width = pcm->width;
 32559                   ascent = pcm->ascent;
 32560                   descent = pcm->descent;
 32561                   lbearing = pcm->lbearing;
 32562                   rbearing = pcm->rbearing;
 32563                   if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
 32564                     {
 32565                       /* Relative composition with or without
 32566                          alternate chars.  */
 32567                       left = (leftmost + rightmost - width) / 2;
 32568                       btm = - descent + boff;
 32569                       if (font->relative_compose
 32570                           && (! CHAR_TABLE_P (Vignore_relative_composition)
 32571                               || NILP (Faref (Vignore_relative_composition,
 32572                                               make_fixnum (ch)))))
 32573                         {
 32574 
 32575                           if (- descent >= font->relative_compose)
 32576                             /* One extra pixel between two glyphs.  */
 32577                             btm = highest + 1;
 32578                           else if (ascent <= 0)
 32579                             /* One extra pixel between two glyphs.  */
 32580                             btm = lowest - 1 - ascent - descent;
 32581                         }
 32582                     }
 32583                   else
 32584                     {
 32585                       /* A composition rule is specified by an integer
 32586                          value that encodes global and new reference
 32587                          points (GREF and NREF).  GREF and NREF are
 32588                          specified by numbers as below:
 32589 
 32590                          0---1---2 -- ascent
 32591                          |       |
 32592                          |       |
 32593                          |       |
 32594                          9--10--11 -- center
 32595                          |       |
 32596                          ---3---4---5--- baseline
 32597                          |       |
 32598                          6---7---8 -- descent
 32599                       */
 32600                       int rule = COMPOSITION_RULE (cmp, i);
 32601                       int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
 32602 
 32603                       COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
 32604                       grefx = gref % 3, nrefx = nref % 3;
 32605                       grefy = gref / 3, nrefy = nref / 3;
 32606                       if (xoff)
 32607                         xoff = font_height * (xoff - 128) / 256;
 32608                       if (yoff)
 32609                         yoff = font_height * (yoff - 128) / 256;
 32610 
 32611                       left = (leftmost
 32612                               + grefx * (rightmost - leftmost) / 2
 32613                               - nrefx * width / 2
 32614                               + xoff);
 32615 
 32616                       btm = ((grefy == 0 ? highest
 32617                               : grefy == 1 ? 0
 32618                               : grefy == 2 ? lowest
 32619                               : (highest + lowest) / 2)
 32620                              - (nrefy == 0 ? ascent + descent
 32621                                 : nrefy == 1 ? descent - boff
 32622                                 : nrefy == 2 ? 0
 32623                                 : (ascent + descent) / 2)
 32624                              + yoff);
 32625                     }
 32626 
 32627                   cmp->offsets[i * 2] = left;
 32628                   cmp->offsets[i * 2 + 1] = btm + descent;
 32629 
 32630                   /* Update the bounding box of the overall glyphs. */
 32631                   if (width > 0)
 32632                     {
 32633                       right = left + width;
 32634                       if (left < leftmost)
 32635                         leftmost = left;
 32636                       if (right > rightmost)
 32637                         rightmost = right;
 32638                     }
 32639                   top = btm + descent + ascent;
 32640                   if (top > highest)
 32641                     highest = top;
 32642                   if (btm < lowest)
 32643                     lowest = btm;
 32644 
 32645                   if (cmp->lbearing > left + lbearing)
 32646                     cmp->lbearing = left + lbearing;
 32647                   if (cmp->rbearing < left + rbearing)
 32648                     cmp->rbearing = left + rbearing;
 32649                 }
 32650             }
 32651 
 32652           /* If there are glyphs whose x-offsets are negative,
 32653              shift all glyphs to the right and make all x-offsets
 32654              non-negative.  */
 32655           if (leftmost < 0)
 32656             {
 32657               for (i = 0; i < cmp->glyph_len; i++)
 32658                 cmp->offsets[i * 2] -= leftmost;
 32659               rightmost -= leftmost;
 32660               cmp->lbearing -= leftmost;
 32661               cmp->rbearing -= leftmost;
 32662             }
 32663 
 32664           if (left_padded && cmp->lbearing < 0)
 32665             {
 32666               for (i = 0; i < cmp->glyph_len; i++)
 32667                 cmp->offsets[i * 2] -= cmp->lbearing;
 32668               rightmost -= cmp->lbearing;
 32669               cmp->rbearing -= cmp->lbearing;
 32670               cmp->lbearing = 0;
 32671             }
 32672           if (right_padded && rightmost < cmp->rbearing)
 32673             {
 32674               rightmost = cmp->rbearing;
 32675             }
 32676 
 32677           cmp->pixel_width = rightmost;
 32678           cmp->ascent = highest;
 32679           cmp->descent = - lowest;
 32680           if (cmp->ascent < font_ascent)
 32681             cmp->ascent = font_ascent;
 32682           if (cmp->descent < font_descent)
 32683             cmp->descent = font_descent;
 32684         }
 32685 
 32686       if (it->glyph_row
 32687           && (cmp->lbearing < 0
 32688               || cmp->rbearing > cmp->pixel_width))
 32689         it->glyph_row->contains_overlapping_glyphs_p = true;
 32690 
 32691       it->pixel_width = cmp->pixel_width;
 32692       it->ascent = it->phys_ascent = cmp->ascent;
 32693       it->descent = it->phys_descent = cmp->descent;
 32694       IT_APPLY_FACE_BOX(it, face);
 32695 
 32696       /* If face has an overline, add the height of the overline
 32697          (1 pixel) and a 1 pixel margin to the character height.  */
 32698       if (face->overline_p)
 32699         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32700 
 32701       take_vertical_position_into_account (it);
 32702       if (it->ascent < 0)
 32703         it->ascent = 0;
 32704       if (it->descent < 0)
 32705         it->descent = 0;
 32706 
 32707       if (it->glyph_row && cmp->glyph_len > 0)
 32708         append_composite_glyph (it);
 32709     }
 32710   else if (it->what == IT_COMPOSITION)
 32711     {
 32712       /* A dynamic (automatic) composition.  */
 32713       struct face *face = FACE_FROM_ID (it->f, it->face_id);
 32714       Lisp_Object gstring;
 32715       struct font_metrics metrics;
 32716 
 32717       it->nglyphs = 1;
 32718 
 32719       gstring = composition_gstring_from_id (it->cmp_it.id);
 32720       it->pixel_width
 32721         = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
 32722                                      &metrics);
 32723       if (it->pixel_width == 0)
 32724         {
 32725           it->glyph_not_available_p = true;
 32726           it->phys_ascent = it->ascent;
 32727           it->phys_descent = it->descent;
 32728           it->pixel_width = face->font->space_width;
 32729         }
 32730       else
 32731         {
 32732           if (it->glyph_row
 32733               && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
 32734             it->glyph_row->contains_overlapping_glyphs_p = true;
 32735           it->ascent = it->phys_ascent = metrics.ascent;
 32736           it->descent = it->phys_descent = metrics.descent;
 32737         }
 32738       IT_APPLY_FACE_BOX(it, face);
 32739 
 32740       /* If face has an overline, add the height of the overline
 32741          (1 pixel) and a 1 pixel margin to the character height.  */
 32742       if (face->overline_p)
 32743         it->ascent += clip_to_bounds (0, overline_margin, 1000000);
 32744       take_vertical_position_into_account (it);
 32745       if (it->ascent < 0)
 32746         it->ascent = 0;
 32747       if (it->descent < 0)
 32748         it->descent = 0;
 32749 
 32750       if (it->glyph_row)
 32751         append_composite_glyph (it);
 32752     }
 32753   else if (it->what == IT_GLYPHLESS)
 32754     produce_glyphless_glyph (it, false, Qnil);
 32755   else if (it->what == IT_IMAGE)
 32756     produce_image_glyph (it);
 32757   else if (it->what == IT_STRETCH)
 32758     produce_stretch_glyph (it);
 32759   else if (it->what == IT_XWIDGET)
 32760     produce_xwidget_glyph (it);
 32761 
 32762  done:
 32763   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
 32764      because this isn't true for images with `:ascent 100'.  */
 32765   eassert (it->ascent >= 0 && it->descent >= 0);
 32766   if (it->area == TEXT_AREA)
 32767     it->current_x += it->pixel_width;
 32768 
 32769   if (extra_line_spacing > 0)
 32770     {
 32771       it->descent += extra_line_spacing;
 32772       if (extra_line_spacing > it->max_extra_line_spacing)
 32773         it->max_extra_line_spacing = extra_line_spacing;
 32774     }
 32775 
 32776   it->max_ascent = max (it->max_ascent, it->ascent);
 32777   it->max_descent = max (it->max_descent, it->descent);
 32778   it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
 32779   it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
 32780 }
 32781 
 32782 /* EXPORT for RIF:
 32783    Output LEN glyphs starting at START at the nominal cursor position.
 32784    Advance the nominal cursor over the text.  UPDATED_ROW is the glyph row
 32785    being updated, and UPDATED_AREA is the area of that row being updated.  */
 32786 
 32787 void
 32788 gui_write_glyphs (struct window *w, struct glyph_row *updated_row,
 32789                 struct glyph *start, enum glyph_row_area updated_area, int len)
 32790 {
 32791   int x, hpos, chpos = w->phys_cursor.hpos;
 32792 
 32793   eassert (updated_row);
 32794   /* When the window is hscrolled, cursor hpos can legitimately be out
 32795      of bounds, but we draw the cursor at the corresponding window
 32796      margin in that case.  */
 32797   if (!updated_row->reversed_p && chpos < 0)
 32798     chpos = 0;
 32799   if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
 32800     chpos = updated_row->used[TEXT_AREA] - 1;
 32801 
 32802   block_input ();
 32803 
 32804   /* Write glyphs.  */
 32805 
 32806   hpos = start - updated_row->glyphs[updated_area];
 32807   x = draw_glyphs (w, w->output_cursor.x,
 32808                    updated_row, updated_area,
 32809                    hpos, hpos + len,
 32810                    DRAW_NORMAL_TEXT, 0);
 32811 
 32812   /* Invalidate old phys cursor if the glyph at its hpos is redrawn.  */
 32813   if (updated_area == TEXT_AREA
 32814       && w->phys_cursor_on_p
 32815       && w->phys_cursor.vpos == w->output_cursor.vpos
 32816       && chpos >= hpos
 32817       && chpos < hpos + len)
 32818     w->phys_cursor_on_p = false;
 32819 
 32820   unblock_input ();
 32821 
 32822   /* Advance the output cursor.  */
 32823   w->output_cursor.hpos += len;
 32824   w->output_cursor.x = x;
 32825 }
 32826 
 32827 
 32828 /* EXPORT for RIF:
 32829    Insert LEN glyphs from START at the nominal cursor position.  */
 32830 
 32831 void
 32832 gui_insert_glyphs (struct window *w, struct glyph_row *updated_row,
 32833                  struct glyph *start, enum glyph_row_area updated_area, int len)
 32834 {
 32835   struct frame *f;
 32836   int line_height, shift_by_width, shifted_region_width;
 32837   struct glyph_row *row;
 32838   struct glyph *glyph;
 32839   int frame_x, frame_y;
 32840   ptrdiff_t hpos;
 32841 
 32842   eassert (updated_row);
 32843   block_input ();
 32844   f = XFRAME (WINDOW_FRAME (w));
 32845 
 32846   /* Get the height of the line we are in.  */
 32847   row = updated_row;
 32848   line_height = row->height;
 32849 
 32850   /* Get the width of the glyphs to insert.  */
 32851   shift_by_width = 0;
 32852   for (glyph = start; glyph < start + len; ++glyph)
 32853     shift_by_width += glyph->pixel_width;
 32854 
 32855   /* Get the width of the region to shift right.  */
 32856   shifted_region_width = (window_box_width (w, updated_area)
 32857                           - w->output_cursor.x
 32858                           - shift_by_width);
 32859 
 32860   /* Shift right.  */
 32861   frame_x = window_box_left (w, updated_area) + w->output_cursor.x;
 32862   frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, w->output_cursor.y);
 32863 
 32864   FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
 32865                                           line_height, shift_by_width);
 32866 
 32867   /* Write the glyphs.  */
 32868   hpos = start - row->glyphs[updated_area];
 32869   draw_glyphs (w, w->output_cursor.x, row, updated_area,
 32870                hpos, hpos + len,
 32871                DRAW_NORMAL_TEXT, 0);
 32872 
 32873   /* Advance the output cursor.  */
 32874   w->output_cursor.hpos += len;
 32875   w->output_cursor.x += shift_by_width;
 32876   unblock_input ();
 32877 }
 32878 
 32879 
 32880 /* EXPORT for RIF:
 32881    Erase the current text line from the nominal cursor position
 32882    (inclusive) to pixel column TO_X (exclusive).  The idea is that
 32883    everything from TO_X onward is already erased.
 32884 
 32885    TO_X is a pixel position relative to UPDATED_AREA of currently
 32886    updated window W.  TO_X == -1 means clear to the end of this area.  */
 32887 
 32888 void
 32889 gui_clear_end_of_line (struct window *w, struct glyph_row *updated_row,
 32890                        enum glyph_row_area updated_area, int to_x)
 32891 {
 32892   struct frame *f;
 32893   int max_x, min_y, max_y;
 32894   int from_x, from_y, to_y;
 32895   struct face *face;
 32896 
 32897   eassert (updated_row);
 32898   f = XFRAME (w->frame);
 32899   face = FACE_FROM_ID_OR_NULL (f, DEFAULT_FACE_ID);
 32900 
 32901   if (updated_row->full_width_p)
 32902     max_x = (WINDOW_PIXEL_WIDTH (w)
 32903              - (updated_row->mode_line_p ? WINDOW_RIGHT_DIVIDER_WIDTH (w) : 0));
 32904   else
 32905     max_x = window_box_width (w, updated_area);
 32906   max_y = window_text_bottom_y (w);
 32907 
 32908   /* TO_X == 0 means don't do anything.  TO_X < 0 means clear to end
 32909      of window.  For TO_X > 0, truncate to end of drawing area.  */
 32910   if (to_x == 0)
 32911     return;
 32912   else if (to_x < 0)
 32913     to_x = max_x;
 32914   else
 32915     to_x = min (to_x, max_x);
 32916 
 32917   to_y = min (max_y, w->output_cursor.y + updated_row->height);
 32918 
 32919   /* Notice if the cursor will be cleared by this operation.  */
 32920   if (!updated_row->full_width_p)
 32921     notice_overwritten_cursor (w, updated_area,
 32922                                w->output_cursor.x, -1,
 32923                                updated_row->y,
 32924                                MATRIX_ROW_BOTTOM_Y (updated_row));
 32925 
 32926   from_x = w->output_cursor.x;
 32927 
 32928   /* Translate to frame coordinates.  */
 32929   if (updated_row->full_width_p)
 32930     {
 32931       from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
 32932       to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
 32933     }
 32934   else
 32935     {
 32936       int area_left = window_box_left (w, updated_area);
 32937       from_x += area_left;
 32938       to_x += area_left;
 32939     }
 32940 
 32941   min_y = WINDOW_TAB_LINE_HEIGHT (w) + WINDOW_HEADER_LINE_HEIGHT (w);
 32942   from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, w->output_cursor.y));
 32943   to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
 32944 
 32945   /* Prevent inadvertently clearing to end of the X window.  */
 32946   if (to_x > from_x && to_y > from_y)
 32947     {
 32948       block_input ();
 32949       FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
 32950                                        to_x - from_x, to_y - from_y);
 32951 
 32952       if (face && !updated_row->stipple_p)
 32953         updated_row->stipple_p = face->stipple;
 32954       unblock_input ();
 32955     }
 32956 }
 32957 
 32958 #endif /* HAVE_WINDOW_SYSTEM */
 32959 
 32960 
 32961 
 32962 /***********************************************************************
 32963                              Cursor types
 32964  ***********************************************************************/
 32965 
 32966 /* Value is the internal representation of the specified cursor type
 32967    ARG.  If type is BAR_CURSOR, return in *WIDTH the specified width
 32968    of the bar cursor.  */
 32969 
 32970 static enum text_cursor_kinds
 32971 get_specified_cursor_type (Lisp_Object arg, int *width)
 32972 {
 32973   enum text_cursor_kinds type;
 32974 
 32975   if (NILP (arg))
 32976     return NO_CURSOR;
 32977 
 32978   if (EQ (arg, Qbox))
 32979     return FILLED_BOX_CURSOR;
 32980 
 32981   if (EQ (arg, Qhollow))
 32982     return HOLLOW_BOX_CURSOR;
 32983 
 32984   if (EQ (arg, Qbar))
 32985     {
 32986       *width = 2;
 32987       return BAR_CURSOR;
 32988     }
 32989 
 32990   if (EQ (arg, Qhbar))
 32991     {
 32992       *width = 2;
 32993       return HBAR_CURSOR;
 32994     }
 32995 
 32996   if (CONSP (arg)
 32997       && RANGED_FIXNUMP (0, XCDR (arg), INT_MAX))
 32998     {
 32999       *width = XFIXNUM (XCDR (arg));
 33000 
 33001       if (EQ (XCAR (arg), Qbox))
 33002           return FILLED_BOX_CURSOR;
 33003       else if (EQ (XCAR (arg), Qbar))
 33004           return BAR_CURSOR;
 33005       else if (EQ (XCAR (arg), Qhbar))
 33006           return HBAR_CURSOR;
 33007     }
 33008 
 33009   /* Treat anything unknown as "hollow box cursor".
 33010      It was bad to signal an error; people have trouble fixing
 33011      .Xdefaults with Emacs, when it has something bad in it.  */
 33012   type = HOLLOW_BOX_CURSOR;
 33013 
 33014   return type;
 33015 }
 33016 
 33017 /* Set the default cursor types for specified frame.  */
 33018 void
 33019 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
 33020 {
 33021   int width = 1;
 33022   Lisp_Object tem;
 33023 
 33024   FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
 33025   FRAME_CURSOR_WIDTH (f) = width;
 33026 
 33027   /* By default, set up the blink-off state depending on the on-state.  */
 33028 
 33029   tem = Fassoc (arg, Vblink_cursor_alist, Qnil);
 33030   if (!NILP (tem))
 33031     {
 33032       FRAME_BLINK_OFF_CURSOR (f)
 33033         = get_specified_cursor_type (XCDR (tem), &width);
 33034       FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
 33035     }
 33036   else
 33037     FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
 33038 
 33039   /* Make sure the cursor gets redrawn.  */
 33040   f->cursor_type_changed = true;
 33041 }
 33042 
 33043 
 33044 #ifdef HAVE_WINDOW_SYSTEM
 33045 
 33046 /* Return the cursor we want to be displayed in window W.  Return
 33047    width of bar/hbar cursor through WIDTH arg.  Return with
 33048    ACTIVE_CURSOR arg set to true if cursor in window W is `active'
 33049    (i.e. if the `system caret' should track this cursor).
 33050 
 33051    In a mini-buffer window, we want the cursor only to appear if we
 33052    are reading input from this window.  For the selected window, we
 33053    want the cursor type given by the frame parameter or buffer local
 33054    setting of cursor-type.  If explicitly marked off, draw no cursor.
 33055    In all other cases, we want a hollow box cursor.  */
 33056 
 33057 static enum text_cursor_kinds
 33058 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
 33059                         bool *active_cursor)
 33060 {
 33061   struct frame *f = XFRAME (w->frame);
 33062   struct buffer *b = XBUFFER (w->contents);
 33063   int cursor_type = DEFAULT_CURSOR;
 33064   Lisp_Object alt_cursor;
 33065   bool non_selected = false;
 33066 
 33067   *active_cursor = true;
 33068 
 33069   /* Echo area */
 33070   if (cursor_in_echo_area
 33071       && FRAME_HAS_MINIBUF_P (f)
 33072       && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
 33073     {
 33074       if (w == XWINDOW (echo_area_window))
 33075         {
 33076           if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
 33077             {
 33078               *width = FRAME_CURSOR_WIDTH (f);
 33079               return FRAME_DESIRED_CURSOR (f);
 33080             }
 33081           else
 33082             return get_specified_cursor_type (BVAR (b, cursor_type), width);
 33083         }
 33084 
 33085       *active_cursor = false;
 33086       non_selected = true;
 33087     }
 33088 
 33089   /* Detect a nonselected window or nonselected frame.  */
 33090   else if (w != XWINDOW (f->selected_window)
 33091            || f != FRAME_DISPLAY_INFO (f)->highlight_frame)
 33092     {
 33093       *active_cursor = false;
 33094 
 33095       if (MINI_WINDOW_P (w) &&
 33096           (minibuf_level == 0
 33097            || is_minibuffer (0, w->contents)))
 33098         return NO_CURSOR;
 33099 
 33100       non_selected = true;
 33101     }
 33102 
 33103   /* Never display a cursor in a window in which cursor-type is nil.  */
 33104   if (NILP (BVAR (b, cursor_type)))
 33105     return NO_CURSOR;
 33106 
 33107   /* Get the normal cursor type for this window.  */
 33108   if (EQ (BVAR (b, cursor_type), Qt))
 33109     {
 33110       cursor_type = FRAME_DESIRED_CURSOR (f);
 33111       *width = FRAME_CURSOR_WIDTH (f);
 33112     }
 33113   else
 33114     cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
 33115 
 33116   /* Use cursor-in-non-selected-windows instead
 33117      for non-selected window or frame.  */
 33118   if (non_selected)
 33119     {
 33120       alt_cursor = BVAR (b, cursor_in_non_selected_windows);
 33121       if (!EQ (Qt, alt_cursor))
 33122         return get_specified_cursor_type (alt_cursor, width);
 33123       /* t means modify the normal cursor type.  */
 33124       if (cursor_type == FILLED_BOX_CURSOR)
 33125         cursor_type = HOLLOW_BOX_CURSOR;
 33126       else if (cursor_type == BAR_CURSOR && *width > 1)
 33127         --*width;
 33128       return cursor_type;
 33129     }
 33130 
 33131   /* Use normal cursor if not blinked off.  */
 33132   if (!w->cursor_off_p)
 33133     {
 33134       if (glyph != NULL && glyph->type == XWIDGET_GLYPH)
 33135         return NO_CURSOR;
 33136       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 33137         {
 33138           if (cursor_type == FILLED_BOX_CURSOR)
 33139             {
 33140               /* Using a block cursor on large images can be very
 33141                  annoying.  So use a hollow cursor for "large" images.
 33142                  If image is not transparent (no mask), also use
 33143                  hollow cursor.  */
 33144               struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 33145               if (img != NULL && IMAGEP (img->spec))
 33146                 {
 33147                   /* Interpret "large" as >SIZExSIZE and >NxN where
 33148                      SIZE is the value from cursor-type of the form
 33149                      (box . SIZE), where N = size of default frame
 33150                      font size.  So, setting cursor-type to (box . 32)
 33151                      should cover most of the "tiny" icons people may
 33152                      use.  */
 33153                   if (!img->mask
 33154                       || (CONSP (BVAR (b, cursor_type))
 33155                           && img->width > max (*width, WINDOW_FRAME_COLUMN_WIDTH (w))
 33156                           && img->height > max (*width, WINDOW_FRAME_LINE_HEIGHT (w))))
 33157                     cursor_type = HOLLOW_BOX_CURSOR;
 33158                 }
 33159             }
 33160           else if (cursor_type != NO_CURSOR)
 33161             {
 33162               /* Display current only supports BOX and HOLLOW cursors for images.
 33163                  So for now, unconditionally use a HOLLOW cursor when cursor is
 33164                  not a solid box cursor.  */
 33165               cursor_type = HOLLOW_BOX_CURSOR;
 33166             }
 33167       }
 33168       return cursor_type;
 33169     }
 33170 
 33171   /* Cursor is blinked off, so determine how to "toggle" it.  */
 33172 
 33173   /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist.  */
 33174   if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist, Qnil), !NILP (alt_cursor)))
 33175     return get_specified_cursor_type (XCDR (alt_cursor), width);
 33176 
 33177   /* Then see if frame has specified a specific blink off cursor type.  */
 33178   if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
 33179     {
 33180       *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
 33181       return FRAME_BLINK_OFF_CURSOR (f);
 33182     }
 33183 
 33184 #if false
 33185   /* Some people liked having a permanently visible blinking cursor,
 33186      while others had very strong opinions against it.  So it was
 33187      decided to remove it.  KFS 2003-09-03 */
 33188 
 33189   /* Finally perform built-in cursor blinking:
 33190        filled box      <->   hollow box
 33191        wide [h]bar     <->   narrow [h]bar
 33192        narrow [h]bar   <->   no cursor
 33193        other type      <->   no cursor  */
 33194 
 33195   if (cursor_type == FILLED_BOX_CURSOR)
 33196     return HOLLOW_BOX_CURSOR;
 33197 
 33198   if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
 33199     {
 33200       *width = 1;
 33201       return cursor_type;
 33202     }
 33203 #endif
 33204 
 33205   return NO_CURSOR;
 33206 }
 33207 
 33208 
 33209 /* Notice when the text cursor of window W has been completely
 33210    overwritten by a drawing operation that outputs glyphs in AREA
 33211    starting at X0 and ending at X1 in the line starting at Y0 and
 33212    ending at Y1.  X coordinates are area-relative.  X1 < 0 means all
 33213    the rest of the line after X0 has been written.  Y coordinates
 33214    are window-relative.  */
 33215 
 33216 static void
 33217 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
 33218                            int x0, int x1, int y0, int y1)
 33219 {
 33220   int cx0, cx1, cy0, cy1;
 33221   struct glyph_row *row;
 33222 
 33223   if (!w->phys_cursor_on_p)
 33224     return;
 33225   if (area != TEXT_AREA)
 33226     return;
 33227 
 33228   if (w->phys_cursor.vpos < 0
 33229       || w->phys_cursor.vpos >= w->current_matrix->nrows
 33230       || (row = w->current_matrix->rows + w->phys_cursor.vpos,
 33231           !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
 33232     return;
 33233 
 33234   if (row->cursor_in_fringe_p)
 33235     {
 33236       row->cursor_in_fringe_p = false;
 33237       draw_fringe_bitmap (w, row, row->reversed_p);
 33238       w->phys_cursor_on_p = false;
 33239       return;
 33240     }
 33241 
 33242   cx0 = w->phys_cursor.x;
 33243   cx1 = cx0 + w->phys_cursor_width;
 33244   if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
 33245     return;
 33246 
 33247   /* The cursor image will be completely removed from the
 33248      screen if the output area intersects the cursor area in
 33249      y-direction.  When we draw in [y0 y1[, and some part of
 33250      the cursor is at y < y0, that part must have been drawn
 33251      before.  When scrolling, the cursor is erased before
 33252      actually scrolling, so we don't come here.  When not
 33253      scrolling, the rows above the old cursor row must have
 33254      changed, and in this case these rows must have written
 33255      over the cursor image.
 33256 
 33257      Likewise if part of the cursor is below y1, with the
 33258      exception of the cursor being in the first blank row at
 33259      the buffer and window end because update_text_area
 33260      doesn't draw that row.  (Except when it does, but
 33261      that's handled in update_text_area.)  */
 33262 
 33263   cy0 = w->phys_cursor.y;
 33264   cy1 = cy0 + w->phys_cursor_height;
 33265   if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
 33266     return;
 33267 
 33268   w->phys_cursor_on_p = false;
 33269 }
 33270 
 33271 #endif /* HAVE_WINDOW_SYSTEM */
 33272 
 33273 
 33274 /************************************************************************
 33275                               Mouse Face
 33276  ************************************************************************/
 33277 
 33278 #ifdef HAVE_WINDOW_SYSTEM
 33279 
 33280 /* EXPORT for RIF:
 33281    Fix the display of area AREA of overlapping row ROW in window W
 33282    with respect to the overlapping part OVERLAPS.  */
 33283 
 33284 void
 33285 gui_fix_overlapping_area (struct window *w, struct glyph_row *row,
 33286                         enum glyph_row_area area, int overlaps)
 33287 {
 33288   int i, x;
 33289 
 33290   block_input ();
 33291 
 33292   x = 0;
 33293   for (i = 0; i < row->used[area];)
 33294     {
 33295       if (row->glyphs[area][i].overlaps_vertically_p)
 33296         {
 33297           int start = i, start_x = x;
 33298 
 33299           do
 33300             {
 33301               x += row->glyphs[area][i].pixel_width;
 33302               ++i;
 33303             }
 33304           while (i < row->used[area]
 33305                  && row->glyphs[area][i].overlaps_vertically_p);
 33306 
 33307           draw_glyphs (w, start_x, row, area,
 33308                        start, i,
 33309                        DRAW_NORMAL_TEXT, overlaps);
 33310         }
 33311       else
 33312         {
 33313           x += row->glyphs[area][i].pixel_width;
 33314           ++i;
 33315         }
 33316     }
 33317 
 33318   unblock_input ();
 33319 }
 33320 
 33321 
 33322 /* EXPORT:
 33323    Draw the cursor glyph of window W in glyph row ROW.  See the
 33324    comment of draw_glyphs for the meaning of HL.  */
 33325 
 33326 void
 33327 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
 33328                         enum draw_glyphs_face hl)
 33329 {
 33330   /* If cursor hpos is out of bounds, don't draw garbage.  This can
 33331      happen in mini-buffer windows when switching between echo area
 33332      glyphs and mini-buffer.  */
 33333   if ((row->reversed_p
 33334        ? (w->phys_cursor.hpos >= 0)
 33335        : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
 33336     {
 33337       bool on_p = w->phys_cursor_on_p;
 33338       int x1;
 33339       int hpos = w->phys_cursor.hpos;
 33340 
 33341       /* When the window is hscrolled, cursor hpos can legitimately be
 33342          out of bounds, but we draw the cursor at the corresponding
 33343          window margin in that case.  */
 33344       if (!row->reversed_p && hpos < 0)
 33345         hpos = 0;
 33346       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33347         hpos = row->used[TEXT_AREA] - 1;
 33348 
 33349       x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
 33350                         hl, 0);
 33351       w->phys_cursor_on_p = on_p;
 33352 
 33353       if (hl == DRAW_CURSOR)
 33354         w->phys_cursor_width = x1 - w->phys_cursor.x;
 33355       /* When we erase the cursor, and ROW is overlapped by other
 33356          rows, make sure that these overlapping parts of other rows
 33357          are redrawn.  */
 33358       else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
 33359         {
 33360           w->phys_cursor_width = x1 - w->phys_cursor.x;
 33361 
 33362           if (row > w->current_matrix->rows
 33363               && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
 33364             gui_fix_overlapping_area (w, row - 1, TEXT_AREA,
 33365                                     OVERLAPS_ERASED_CURSOR);
 33366 
 33367           if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
 33368               && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
 33369             gui_fix_overlapping_area (w, row + 1, TEXT_AREA,
 33370                                     OVERLAPS_ERASED_CURSOR);
 33371         }
 33372     }
 33373 }
 33374 
 33375 
 33376 /* Erase the image of a cursor of window W from the screen.  */
 33377 
 33378 void
 33379 erase_phys_cursor (struct window *w)
 33380 {
 33381   struct frame *f = XFRAME (w->frame);
 33382   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 33383   int hpos = w->phys_cursor.hpos;
 33384   int vpos = w->phys_cursor.vpos;
 33385   bool mouse_face_here_p = false;
 33386   struct glyph_matrix *active_glyphs = w->current_matrix;
 33387   struct glyph_row *cursor_row;
 33388   struct glyph *cursor_glyph;
 33389   enum draw_glyphs_face hl;
 33390 
 33391   /* No cursor displayed or row invalidated => nothing to do on the
 33392      screen.  */
 33393   if (w->phys_cursor_type == NO_CURSOR)
 33394     goto mark_cursor_off;
 33395 
 33396   /* VPOS >= active_glyphs->nrows means that window has been resized.
 33397      Don't bother to erase the cursor.  */
 33398   if (vpos >= active_glyphs->nrows)
 33399     goto mark_cursor_off;
 33400 
 33401   /* If row containing cursor is marked invalid, there is nothing we
 33402      can do.  */
 33403   cursor_row = MATRIX_ROW (active_glyphs, vpos);
 33404   if (!cursor_row->enabled_p)
 33405     goto mark_cursor_off;
 33406 
 33407   /* If line spacing is > 0, old cursor may only be partially visible in
 33408      window after split-window.  So adjust visible height.  */
 33409   cursor_row->visible_height = min (cursor_row->visible_height,
 33410                                     window_text_bottom_y (w) - cursor_row->y);
 33411 
 33412   /* If row is completely invisible, don't attempt to delete a cursor which
 33413      isn't there.  This can happen if cursor is at top of a window, and
 33414      we switch to a buffer with a header line in that window.  */
 33415   if (cursor_row->visible_height <= 0)
 33416     goto mark_cursor_off;
 33417 
 33418   /* If cursor is in the fringe, erase by drawing actual bitmap there.  */
 33419   if (cursor_row->cursor_in_fringe_p)
 33420     {
 33421       cursor_row->cursor_in_fringe_p = false;
 33422       draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
 33423       goto mark_cursor_off;
 33424     }
 33425 
 33426   /* This can happen when the new row is shorter than the old one.
 33427      In this case, either draw_glyphs or clear_end_of_line
 33428      should have cleared the cursor.  Note that we wouldn't be
 33429      able to erase the cursor in this case because we don't have a
 33430      cursor glyph at hand.  */
 33431   if ((cursor_row->reversed_p
 33432        ? (w->phys_cursor.hpos < 0)
 33433        : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
 33434     goto mark_cursor_off;
 33435 
 33436   /* When the window is hscrolled, cursor hpos can legitimately be out
 33437      of bounds, but we draw the cursor at the corresponding window
 33438      margin in that case.  */
 33439   if (!cursor_row->reversed_p && hpos < 0)
 33440     hpos = 0;
 33441   if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
 33442     hpos = cursor_row->used[TEXT_AREA] - 1;
 33443 
 33444   /* If the cursor is in the mouse face area, redisplay that when
 33445      we clear the cursor.  */
 33446   if (! NILP (hlinfo->mouse_face_window)
 33447       && coords_in_mouse_face_p (w, hpos, vpos)
 33448       /* Don't redraw the cursor's spot in mouse face if it is at the
 33449          end of a line (on a newline).  The cursor appears there, but
 33450          mouse highlighting does not.  */
 33451       && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
 33452     mouse_face_here_p = true;
 33453 
 33454 #ifdef HAVE_WINDOW_SYSTEM
 33455   /* Since erasing the phys cursor will probably lead to corruption of
 33456      the mouse face display if the glyph's pixel_width is not kept up
 33457      to date with the :box property of the mouse face, just redraw the
 33458      mouse face.  */
 33459   if (FRAME_WINDOW_P (WINDOW_XFRAME (w)) && mouse_face_here_p)
 33460     {
 33461       w->phys_cursor_on_p = false;
 33462       w->phys_cursor_type = NO_CURSOR;
 33463       show_mouse_face (MOUSE_HL_INFO (WINDOW_XFRAME (w)), DRAW_MOUSE_FACE);
 33464       return;
 33465     }
 33466 #endif
 33467 
 33468   /* Maybe clear the display under the cursor.  */
 33469   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
 33470     {
 33471       int x, y;
 33472       int tab_line_height = WINDOW_TAB_LINE_HEIGHT (w);
 33473       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
 33474       int width;
 33475 
 33476       cursor_glyph = get_phys_cursor_glyph (w);
 33477       if (cursor_glyph == NULL)
 33478         goto mark_cursor_off;
 33479 
 33480       width = cursor_glyph->pixel_width;
 33481       x = w->phys_cursor.x;
 33482       if (x < 0)
 33483         {
 33484           width += x;
 33485           x = 0;
 33486         }
 33487       width = min (width, window_box_width (w, TEXT_AREA) - x);
 33488       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (tab_line_height, max (header_line_height, cursor_row->y)));
 33489       x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 33490 
 33491       if (width > 0)
 33492         FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
 33493     }
 33494 
 33495   /* Erase the cursor by redrawing the character underneath it.  */
 33496   if (mouse_face_here_p)
 33497     hl = DRAW_MOUSE_FACE;
 33498   else
 33499     hl = DRAW_NORMAL_TEXT;
 33500   draw_phys_cursor_glyph (w, cursor_row, hl);
 33501 
 33502  mark_cursor_off:
 33503   w->phys_cursor_on_p = false;
 33504   w->phys_cursor_type = NO_CURSOR;
 33505 }
 33506 
 33507 
 33508 /* Display or clear cursor of window W.  If !ON, clear the cursor.
 33509    If ON, display the cursor; where to put the cursor is specified by
 33510    HPOS, VPOS, X and Y.  */
 33511 
 33512 void
 33513 display_and_set_cursor (struct window *w, bool on,
 33514                         int hpos, int vpos, int x, int y)
 33515 {
 33516   struct frame *f = XFRAME (w->frame);
 33517   int new_cursor_type;
 33518   int new_cursor_width UNINIT;
 33519   bool active_cursor;
 33520   struct glyph_row *glyph_row;
 33521   struct glyph *glyph;
 33522 
 33523   /* This is pointless on invisible frames, and dangerous on garbaged
 33524      windows and frames; in the latter case, the frame or window may
 33525      be in the midst of changing its size, and x and y may be off the
 33526      window.  */
 33527   if (! FRAME_REDISPLAY_P (f)
 33528       || vpos >= w->current_matrix->nrows
 33529       || hpos >= w->current_matrix->matrix_w)
 33530     return;
 33531 
 33532   /* If cursor is off and we want it off, return quickly.  */
 33533   if (!on && !w->phys_cursor_on_p)
 33534     return;
 33535 
 33536   glyph_row = MATRIX_ROW (w->current_matrix, vpos);
 33537   /* If cursor row is not enabled, we don't really know where to
 33538      display the cursor.  */
 33539   if (!glyph_row->enabled_p)
 33540     {
 33541       w->phys_cursor_on_p = false;
 33542       return;
 33543     }
 33544 
 33545   /* A frame might be marked garbaged even though its cursor position
 33546      is correct, and will not change upon subsequent redisplay.  This
 33547      happens in some rare situations, like toggling the sort order in
 33548      Dired windows.  We've already established that VPOS is valid, so
 33549      it shouldn't do any harm to record the cursor position, as we are
 33550      going to return without acting on it anyway.  Otherwise, expose
 33551      events might come in and call update_window_cursor, which will
 33552      blindly use outdated values in w->phys_cursor.  */
 33553   if (FRAME_GARBAGED_P (f))
 33554     {
 33555       if (on)
 33556         {
 33557           w->phys_cursor.x = x;
 33558           w->phys_cursor.y = glyph_row->y;
 33559           w->phys_cursor.hpos = hpos;
 33560           w->phys_cursor.vpos = vpos;
 33561         }
 33562       return;
 33563     }
 33564 
 33565   glyph = NULL;
 33566   if (0 <= hpos && hpos < glyph_row->used[TEXT_AREA])
 33567     glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
 33568 
 33569   eassert (input_blocked_p ());
 33570 
 33571   /* Set new_cursor_type to the cursor we want to be displayed.  */
 33572   new_cursor_type = get_window_cursor_type (w, glyph,
 33573                                             &new_cursor_width, &active_cursor);
 33574 
 33575   /* If cursor is currently being shown and we don't want it to be or
 33576      it is in the wrong place, or the cursor type is not what we want,
 33577      erase it.  */
 33578   if (w->phys_cursor_on_p
 33579       && (!on
 33580           || w->phys_cursor.x != x
 33581           || w->phys_cursor.y != y
 33582           /* HPOS can be negative in R2L rows whose
 33583              exact_window_width_line_p flag is set (i.e. their newline
 33584              would "overflow into the fringe").  */
 33585           || hpos < 0
 33586           || new_cursor_type != w->phys_cursor_type
 33587           || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
 33588               && new_cursor_width != w->phys_cursor_width)))
 33589     erase_phys_cursor (w);
 33590 
 33591   /* Don't check phys_cursor_on_p here because that flag is only set
 33592      to false in some cases where we know that the cursor has been
 33593      completely erased, to avoid the extra work of erasing the cursor
 33594      twice.  In other words, phys_cursor_on_p can be true and the cursor
 33595      still not be visible, or it has only been partly erased.  */
 33596   if (on)
 33597     {
 33598       w->phys_cursor_ascent = glyph_row->ascent;
 33599       w->phys_cursor_height = glyph_row->height;
 33600 
 33601       /* Set phys_cursor_.* before x_draw_.* is called because some
 33602          of them may need the information.  */
 33603       w->phys_cursor.x = x;
 33604       w->phys_cursor.y = glyph_row->y;
 33605       w->phys_cursor.hpos = hpos;
 33606       w->phys_cursor.vpos = vpos;
 33607     }
 33608 
 33609   FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
 33610                                      new_cursor_type, new_cursor_width,
 33611                                      on, active_cursor);
 33612 }
 33613 
 33614 
 33615 /* Switch the display of W's cursor on or off, according to the value
 33616    of ON.  */
 33617 
 33618 static void
 33619 update_window_cursor (struct window *w, bool on)
 33620 {
 33621   /* Don't update cursor in windows whose frame is in the process
 33622      of being deleted.  */
 33623   if (w->current_matrix)
 33624     {
 33625       int hpos = w->phys_cursor.hpos;
 33626       int vpos = w->phys_cursor.vpos;
 33627       struct glyph_row *row;
 33628 
 33629       if (vpos >= w->current_matrix->nrows
 33630           || hpos >= w->current_matrix->matrix_w)
 33631         return;
 33632 
 33633       row = MATRIX_ROW (w->current_matrix, vpos);
 33634 
 33635       /* When the window is hscrolled, cursor hpos can legitimately be
 33636          out of bounds, but we draw the cursor at the corresponding
 33637          window margin in that case.  */
 33638       if (!row->reversed_p && hpos < 0)
 33639         hpos = 0;
 33640       if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33641         hpos = row->used[TEXT_AREA] - 1;
 33642 
 33643       block_input ();
 33644       display_and_set_cursor (w, on, hpos, vpos,
 33645                               w->phys_cursor.x, w->phys_cursor.y);
 33646       unblock_input ();
 33647     }
 33648 }
 33649 
 33650 
 33651 /* Call update_window_cursor with parameter ON_P on all leaf windows
 33652    in the window tree rooted at W.  */
 33653 
 33654 static void
 33655 update_cursor_in_window_tree (struct window *w, bool on_p)
 33656 {
 33657   while (w)
 33658     {
 33659       if (WINDOWP (w->contents))
 33660         update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
 33661       else
 33662         update_window_cursor (w, on_p);
 33663 
 33664       w = NILP (w->next) ? 0 : XWINDOW (w->next);
 33665     }
 33666 }
 33667 
 33668 
 33669 /* EXPORT:
 33670    Display the cursor on window W, or clear it, according to ON_P.
 33671    Don't change the cursor's position.  */
 33672 
 33673 void
 33674 gui_update_cursor (struct frame *f, bool on_p)
 33675 {
 33676   update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
 33677 }
 33678 
 33679 
 33680 /* EXPORT:
 33681    Clear the cursor of window W to background color, and mark the
 33682    cursor as not shown.  This is used when the text where the cursor
 33683    is about to be rewritten.  */
 33684 
 33685 void
 33686 gui_clear_cursor (struct window *w)
 33687 {
 33688   if (FRAME_REDISPLAY_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
 33689     update_window_cursor (w, false);
 33690 }
 33691 
 33692 #endif /* HAVE_WINDOW_SYSTEM */
 33693 
 33694 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
 33695    and MSDOS.  */
 33696 static void
 33697 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
 33698                           int start_hpos, int end_hpos,
 33699                           enum draw_glyphs_face draw)
 33700 {
 33701 #ifdef HAVE_WINDOW_SYSTEM
 33702   if (FRAME_WINDOW_P (XFRAME (w->frame)))
 33703     {
 33704       draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
 33705       return;
 33706     }
 33707 #endif
 33708 
 33709   tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
 33710 }
 33711 
 33712 /* Display the active region described by mouse_face_* according to DRAW.  */
 33713 
 33714 static void
 33715 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 33716 {
 33717   /* Don't bother doing anything if the mouse-face window is not set
 33718      up.  */
 33719   if (!WINDOWP (hlinfo->mouse_face_window))
 33720     return;
 33721 
 33722   struct window *w = XWINDOW (hlinfo->mouse_face_window);
 33723   struct frame *f = XFRAME (WINDOW_FRAME (w));
 33724 
 33725   /* Don't bother doing anything if we are on a wrong frame.  */
 33726   if (f != hlinfo->mouse_face_mouse_frame)
 33727     return;
 33728 
 33729   if (/* If window is in the process of being destroyed, don't bother
 33730          to do anything.  */
 33731       w->current_matrix != NULL
 33732       /* Don't update mouse highlight if hidden.  */
 33733       && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
 33734       /* Recognize when we are called to operate on rows that don't exist
 33735          anymore.  This can happen when a window is split.  */
 33736       && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
 33737     {
 33738       bool phys_cursor_on_p = w->phys_cursor_on_p;
 33739 #ifdef HAVE_WINDOW_SYSTEM
 33740       int mouse_off = 0;
 33741 #endif
 33742       struct glyph_row *row, *first, *last;
 33743 
 33744       first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
 33745       last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
 33746 
 33747       for (row = first; row <= last && row->enabled_p; ++row)
 33748         {
 33749           int start_hpos, end_hpos, start_x;
 33750 
 33751           /* For all but the first row, the highlight starts at column 0.  */
 33752           if (row == first)
 33753             {
 33754               /* R2L rows have BEG and END in reversed order, but the
 33755                  screen drawing geometry is always left to right.  So
 33756                  we need to mirror the beginning and end of the
 33757                  highlighted area in R2L rows.  */
 33758               if (!row->reversed_p)
 33759                 {
 33760                   start_hpos = hlinfo->mouse_face_beg_col;
 33761                   start_x = hlinfo->mouse_face_beg_x;
 33762                 }
 33763               else if (row == last)
 33764                 {
 33765                   start_hpos = hlinfo->mouse_face_end_col;
 33766                   start_x = hlinfo->mouse_face_end_x;
 33767                 }
 33768               else
 33769                 {
 33770                   start_hpos = 0;
 33771                   start_x = 0;
 33772                 }
 33773             }
 33774           else if (row->reversed_p && row == last)
 33775             {
 33776               start_hpos = hlinfo->mouse_face_end_col;
 33777               start_x = hlinfo->mouse_face_end_x;
 33778             }
 33779           else
 33780             {
 33781               start_hpos = 0;
 33782               start_x = 0;
 33783             }
 33784 
 33785           if (row == last)
 33786             {
 33787               if (!row->reversed_p)
 33788                 end_hpos = hlinfo->mouse_face_end_col;
 33789               else if (row == first)
 33790                 end_hpos = hlinfo->mouse_face_beg_col;
 33791               else
 33792                 {
 33793                   end_hpos = row->used[TEXT_AREA];
 33794                   if (draw == DRAW_NORMAL_TEXT)
 33795                     row->fill_line_p = true; /* Clear to end of line.  */
 33796                 }
 33797             }
 33798           else if (row->reversed_p && row == first)
 33799             end_hpos = hlinfo->mouse_face_beg_col;
 33800           else
 33801             {
 33802               end_hpos = row->used[TEXT_AREA];
 33803               if (draw == DRAW_NORMAL_TEXT)
 33804                 row->fill_line_p = true; /* Clear to end of line.  */
 33805             }
 33806 
 33807           if (end_hpos > start_hpos)
 33808             {
 33809               draw_row_with_mouse_face (w, start_x, row,
 33810                                         start_hpos, end_hpos, draw);
 33811 
 33812               row->mouse_face_p
 33813                 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
 33814             }
 33815 #ifdef HAVE_WINDOW_SYSTEM
 33816           /* Compute the cursor offset due to mouse-highlight.  */
 33817           if ((MATRIX_ROW_VPOS (row, w->current_matrix) == w->phys_cursor.vpos)
 33818               /* But not when highlighting a pseudo window, such as
 33819                  the toolbar, which can't have a cursor anyway.  */
 33820               && !w->pseudo_window_p
 33821               && draw == DRAW_MOUSE_FACE)
 33822             get_cursor_offset_for_mouse_face (w, row, &mouse_off);
 33823 #endif
 33824         }
 33825 
 33826       /* When we've written over the cursor, arrange for it to
 33827          be displayed again.  */
 33828       if (FRAME_WINDOW_P (f)
 33829           && phys_cursor_on_p && !w->phys_cursor_on_p)
 33830         {
 33831 #ifdef HAVE_WINDOW_SYSTEM
 33832           int hpos = w->phys_cursor.hpos;
 33833           int old_phys_cursor_x = w->phys_cursor.x;
 33834 
 33835           /* When the window is hscrolled, cursor hpos can legitimately be
 33836              out of bounds, but we draw the cursor at the corresponding
 33837              window margin in that case.  */
 33838           if (!row->reversed_p && hpos < 0)
 33839             hpos = 0;
 33840           if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33841             hpos = row->used[TEXT_AREA] - 1;
 33842 
 33843           block_input ();
 33844           display_and_set_cursor (w, true, hpos, w->phys_cursor.vpos,
 33845                                   w->phys_cursor.x + mouse_off,
 33846                                   w->phys_cursor.y);
 33847           /* Restore the original cursor coordinates, perhaps modified
 33848              to account for mouse-highlight.  */
 33849           w->phys_cursor.x = old_phys_cursor_x;
 33850           unblock_input ();
 33851 #endif  /* HAVE_WINDOW_SYSTEM */
 33852         }
 33853     }
 33854 
 33855 #ifdef HAVE_WINDOW_SYSTEM
 33856   /* Change the mouse cursor.  */
 33857   if (FRAME_WINDOW_P (f) && NILP (track_mouse))
 33858     {
 33859       if (draw == DRAW_NORMAL_TEXT
 33860 #ifndef HAVE_EXT_TOOL_BAR
 33861           && !EQ (hlinfo->mouse_face_window, f->tool_bar_window)
 33862 #endif
 33863           && !EQ (hlinfo->mouse_face_window, f->tab_bar_window))
 33864         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->text_cursor);
 33865       else
 33866       if (draw == DRAW_MOUSE_FACE)
 33867         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->hand_cursor);
 33868       else
 33869         FRAME_RIF (f)->define_frame_cursor (f, FRAME_OUTPUT_DATA (f)->nontext_cursor);
 33870     }
 33871 #endif  /* HAVE_WINDOW_SYSTEM */
 33872 }
 33873 
 33874 /* EXPORT:
 33875    Clear out the mouse-highlighted active region.
 33876    Redraw it un-highlighted first.  Value is true if mouse
 33877    face was actually drawn unhighlighted.  */
 33878 
 33879 bool
 33880 clear_mouse_face (Mouse_HLInfo *hlinfo)
 33881 {
 33882   bool cleared
 33883     = !hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window);
 33884   if (cleared)
 33885     show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
 33886   hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
 33887   hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
 33888   hlinfo->mouse_face_window = Qnil;
 33889   hlinfo->mouse_face_overlay = Qnil;
 33890   return cleared;
 33891 }
 33892 
 33893 /* Return true if the coordinates HPOS and VPOS on windows W are
 33894    within the mouse face on that window.  */
 33895 static bool
 33896 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
 33897 {
 33898   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 33899 
 33900   /* Quickly resolve the easy cases.  */
 33901   if (!(WINDOWP (hlinfo->mouse_face_window)
 33902         && XWINDOW (hlinfo->mouse_face_window) == w))
 33903     return false;
 33904   if (vpos < hlinfo->mouse_face_beg_row
 33905       || vpos > hlinfo->mouse_face_end_row)
 33906     return false;
 33907   if (vpos > hlinfo->mouse_face_beg_row
 33908       && vpos < hlinfo->mouse_face_end_row)
 33909     return true;
 33910 
 33911   if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
 33912     {
 33913       if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33914         {
 33915           if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
 33916             return true;
 33917         }
 33918       else if ((vpos == hlinfo->mouse_face_beg_row
 33919                 && hpos >= hlinfo->mouse_face_beg_col)
 33920                || (vpos == hlinfo->mouse_face_end_row
 33921                    && hpos < hlinfo->mouse_face_end_col))
 33922         return true;
 33923     }
 33924   else
 33925     {
 33926        if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
 33927         {
 33928           if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
 33929             return true;
 33930         }
 33931       else if ((vpos == hlinfo->mouse_face_beg_row
 33932                 && hpos <= hlinfo->mouse_face_beg_col)
 33933                || (vpos == hlinfo->mouse_face_end_row
 33934                    && hpos > hlinfo->mouse_face_end_col))
 33935         return true;
 33936     }
 33937   return false;
 33938 }
 33939 
 33940 
 33941 /* EXPORT:
 33942    True if physical cursor of window W is within mouse face.  */
 33943 
 33944 bool
 33945 cursor_in_mouse_face_p (struct window *w)
 33946 {
 33947   int vpos = w->phys_cursor.vpos;
 33948 
 33949   /* If the cursor is outside the matrix glyph rows, it cannot be
 33950      within the mouse face.  */
 33951   if (!(0 <= vpos && vpos < w->current_matrix->nrows))
 33952     return false;
 33953 
 33954   int hpos = w->phys_cursor.hpos;
 33955   struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
 33956 
 33957   /* When the window is hscrolled, cursor hpos can legitimately be out
 33958      of bounds, but we draw the cursor at the corresponding window
 33959      margin in that case.  */
 33960   if (!row->reversed_p && hpos < 0)
 33961     hpos = 0;
 33962   if (row->reversed_p && hpos >= row->used[TEXT_AREA])
 33963     hpos = row->used[TEXT_AREA] - 1;
 33964 
 33965   return coords_in_mouse_face_p (w, hpos, vpos);
 33966 }
 33967 
 33968 
 33969 
 33970 /* Find the glyph rows START_ROW and END_ROW of window W that display
 33971    characters between buffer positions START_CHARPOS and END_CHARPOS
 33972    (excluding END_CHARPOS).  DISP_STRING is a display string that
 33973    covers these buffer positions.  This is similar to
 33974    row_containing_pos, but is more accurate when bidi reordering makes
 33975    buffer positions change non-linearly with glyph rows.  */
 33976 static void
 33977 rows_from_pos_range (struct window *w,
 33978                      ptrdiff_t start_charpos, ptrdiff_t end_charpos,
 33979                      Lisp_Object disp_string,
 33980                      struct glyph_row **start, struct glyph_row **end)
 33981 {
 33982   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 33983   int last_y = window_text_bottom_y (w);
 33984   struct glyph_row *row;
 33985 
 33986   *start = NULL;
 33987   *end = NULL;
 33988 
 33989   while (!first->enabled_p
 33990          && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
 33991     first++;
 33992 
 33993   /* Find the START row.  */
 33994   for (row = first;
 33995        row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
 33996        row++)
 33997     {
 33998       /* A row can potentially be the START row if the range of the
 33999          characters it displays intersects the range
 34000          [START_CHARPOS..END_CHARPOS).  */
 34001       if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
 34002               && end_charpos < MATRIX_ROW_START_CHARPOS (row))
 34003              /* See the commentary in row_containing_pos, for the
 34004                 explanation of the complicated way to check whether
 34005                 some position is beyond the end of the characters
 34006                 displayed by a row.  */
 34007              || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
 34008                   || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
 34009                       && !row->ends_at_zv_p
 34010                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
 34011                  && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
 34012                      || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
 34013                          && !row->ends_at_zv_p
 34014                          && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
 34015         {
 34016           /* Found a candidate row.  Now make sure at least one of the
 34017              glyphs it displays has a charpos from the range
 34018              [START_CHARPOS..END_CHARPOS).
 34019 
 34020              This is not obvious because bidi reordering could make
 34021              buffer positions of a row be 1,2,3,102,101,100, and if we
 34022              want to highlight characters in [50..60), we don't want
 34023              this row, even though [50..60) does intersect [1..103),
 34024              the range of character positions given by the row's start
 34025              and end positions.  */
 34026           struct glyph *g = row->glyphs[TEXT_AREA];
 34027           struct glyph *e = g + row->used[TEXT_AREA];
 34028 
 34029           while (g < e)
 34030             {
 34031               if (((BUFFERP (g->object) || NILP (g->object))
 34032                    && start_charpos <= g->charpos && g->charpos < end_charpos)
 34033                   /* A glyph that comes from DISP_STRING is by
 34034                      definition to be highlighted.  */
 34035                   || EQ (g->object, disp_string))
 34036                 *start = row;
 34037               g++;
 34038             }
 34039           if (*start)
 34040             break;
 34041         }
 34042     }
 34043 
 34044   /* Find the END row.  */
 34045   if (!*start
 34046       /* If the last row is partially visible, start looking for END
 34047          from that row, instead of starting from FIRST.  */
 34048       && !(row->enabled_p
 34049            && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
 34050     row = first;
 34051   for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
 34052     {
 34053       struct glyph_row *next = row + 1;
 34054       ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
 34055 
 34056       if (!next->enabled_p
 34057           || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
 34058           /* The first row >= START whose range of displayed characters
 34059              does NOT intersect the range [START_CHARPOS..END_CHARPOS]
 34060              is the row END + 1.  */
 34061           || (start_charpos < next_start
 34062               && end_charpos < next_start)
 34063           || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
 34064                || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
 34065                    && !next->ends_at_zv_p
 34066                    && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
 34067               && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
 34068                   || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
 34069                       && !next->ends_at_zv_p
 34070                       && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
 34071         {
 34072           *end = row;
 34073           break;
 34074         }
 34075       else
 34076         {
 34077           /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
 34078              but none of the characters it displays are in the range, it is
 34079              also END + 1. */
 34080           struct glyph *g = next->glyphs[TEXT_AREA];
 34081           struct glyph *s = g;
 34082           struct glyph *e = g + next->used[TEXT_AREA];
 34083 
 34084           while (g < e)
 34085             {
 34086               if (((BUFFERP (g->object) || NILP (g->object))
 34087                    && ((start_charpos <= g->charpos && g->charpos < end_charpos)
 34088                        /* If the buffer position of the first glyph in
 34089                           the row is equal to END_CHARPOS, it means
 34090                           the last character to be highlighted is the
 34091                           newline of ROW, and we must consider NEXT as
 34092                           END, not END+1.  */
 34093                        || (((!next->reversed_p && g == s)
 34094                             || (next->reversed_p && g == e - 1))
 34095                            && (g->charpos == end_charpos
 34096                                /* Special case for when NEXT is an
 34097                                   empty line at ZV.  */
 34098                                || (g->charpos == -1
 34099                                    && !row->ends_at_zv_p
 34100                                    && next_start == end_charpos)))))
 34101                   /* A glyph that comes from DISP_STRING is by
 34102                      definition to be highlighted.  */
 34103                   || EQ (g->object, disp_string))
 34104                 break;
 34105               g++;
 34106             }
 34107           if (g == e)
 34108             {
 34109               *end = row;
 34110               break;
 34111             }
 34112           /* The first row that ends at ZV must be the last to be
 34113              highlighted.  */
 34114           else if (next->ends_at_zv_p)
 34115             {
 34116               *end = next;
 34117               break;
 34118             }
 34119         }
 34120     }
 34121 }
 34122 
 34123 /* This function sets the mouse_face_* elements of HLINFO, assuming
 34124    the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
 34125    window WINDOW.  START_CHARPOS and END_CHARPOS are buffer positions
 34126    for the overlay or run of text properties specifying the mouse
 34127    face.  BEFORE_STRING and AFTER_STRING, if non-nil, are a
 34128    before-string and after-string that must also be highlighted.
 34129    DISP_STRING, if non-nil, is a display string that may cover some
 34130    or all of the highlighted text.  */
 34131 
 34132 static void
 34133 mouse_face_from_buffer_pos (Lisp_Object window,
 34134                             Mouse_HLInfo *hlinfo,
 34135                             ptrdiff_t mouse_charpos,
 34136                             ptrdiff_t start_charpos,
 34137                             ptrdiff_t end_charpos,
 34138                             Lisp_Object before_string,
 34139                             Lisp_Object after_string,
 34140                             Lisp_Object disp_string)
 34141 {
 34142   struct window *w = XWINDOW (window);
 34143   struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34144   struct glyph_row *r1, *r2;
 34145   struct glyph *glyph, *end;
 34146   ptrdiff_t ignore, pos;
 34147   int x;
 34148 
 34149   eassert (NILP (disp_string) || STRINGP (disp_string));
 34150   eassert (NILP (before_string) || STRINGP (before_string));
 34151   eassert (NILP (after_string) || STRINGP (after_string));
 34152 
 34153   /* Find the rows corresponding to START_CHARPOS and END_CHARPOS.  */
 34154   rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
 34155   if (r1 == NULL)
 34156     r1 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34157   /* If the before-string or display-string contains newlines,
 34158      rows_from_pos_range skips to its last row.  Move back.  */
 34159   if (!NILP (before_string) || !NILP (disp_string))
 34160     {
 34161       struct glyph_row *prev;
 34162       while ((prev = r1 - 1, prev >= first)
 34163              && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
 34164              && prev->used[TEXT_AREA] > 0)
 34165         {
 34166           struct glyph *beg = prev->glyphs[TEXT_AREA];
 34167           glyph = beg + prev->used[TEXT_AREA];
 34168           while (--glyph >= beg && NILP (glyph->object));
 34169           if (glyph < beg
 34170               || !(EQ (glyph->object, before_string)
 34171                    || EQ (glyph->object, disp_string)))
 34172             break;
 34173           r1 = prev;
 34174         }
 34175     }
 34176   if (r2 == NULL)
 34177     {
 34178       r2 = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34179       hlinfo->mouse_face_past_end = true;
 34180     }
 34181   else if (!NILP (after_string))
 34182     {
 34183       /* If the after-string has newlines, advance to its last row.  */
 34184       struct glyph_row *next;
 34185       struct glyph_row *last
 34186         = MATRIX_ROW (w->current_matrix, w->window_end_vpos);
 34187 
 34188       for (next = r2 + 1;
 34189            next <= last
 34190              && next->used[TEXT_AREA] > 0
 34191              && EQ (next->glyphs[TEXT_AREA]->object, after_string);
 34192            ++next)
 34193         r2 = next;
 34194     }
 34195   /* The rest of the display engine assumes that mouse_face_beg_row is
 34196      either above mouse_face_end_row or identical to it.  But with
 34197      bidi-reordered continued lines, the row for START_CHARPOS could
 34198      be below the row for END_CHARPOS.  If so, swap the rows and store
 34199      them in correct order.  */
 34200   if (r1->y > r2->y)
 34201     {
 34202       struct glyph_row *tem = r2;
 34203 
 34204       r2 = r1;
 34205       r1 = tem;
 34206     }
 34207 
 34208   hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
 34209   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
 34210 
 34211   /* For a bidi-reordered row, the positions of BEFORE_STRING,
 34212      AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
 34213      could be anywhere in the row and in any order.  The strategy
 34214      below is to find the leftmost and the rightmost glyph that
 34215      belongs to either of these 3 strings, or whose position is
 34216      between START_CHARPOS and END_CHARPOS, and highlight all the
 34217      glyphs between those two.  This may cover more than just the text
 34218      between START_CHARPOS and END_CHARPOS if the range of characters
 34219      strides the bidi level boundary, e.g. if the beginning is in R2L
 34220      text while the end is in L2R text or vice versa.  */
 34221   if (!r1->reversed_p)
 34222     {
 34223       /* This row is in a left to right paragraph.  Scan it left to
 34224          right.  */
 34225       glyph = r1->glyphs[TEXT_AREA];
 34226       end = glyph + r1->used[TEXT_AREA];
 34227       x = r1->x;
 34228 
 34229       /* Skip truncation glyphs at the start of the glyph row.  */
 34230       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34231         for (; glyph < end
 34232                && NILP (glyph->object)
 34233                && glyph->charpos < 0;
 34234              ++glyph)
 34235           x += glyph->pixel_width;
 34236 
 34237       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34238          or DISP_STRING, and the first glyph from buffer whose
 34239          position is between START_CHARPOS and END_CHARPOS.  */
 34240       for (; glyph < end
 34241              && !NILP (glyph->object)
 34242              && !EQ (glyph->object, disp_string)
 34243              && !(BUFFERP (glyph->object)
 34244                   && (glyph->charpos >= start_charpos
 34245                       && glyph->charpos < end_charpos));
 34246            ++glyph)
 34247         {
 34248           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34249              are present at buffer positions between START_CHARPOS and
 34250              END_CHARPOS, or if they come from an overlay.  */
 34251           if (EQ (glyph->object, before_string))
 34252             {
 34253               pos = string_buffer_position (before_string,
 34254                                             start_charpos);
 34255               /* If pos == 0, it means before_string came from an
 34256                  overlay, not from a buffer position.  */
 34257               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34258                 break;
 34259             }
 34260           else if (EQ (glyph->object, after_string))
 34261             {
 34262               pos = string_buffer_position (after_string, end_charpos);
 34263               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34264                 break;
 34265             }
 34266           x += glyph->pixel_width;
 34267         }
 34268       hlinfo->mouse_face_beg_x = x;
 34269       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34270     }
 34271   else
 34272     {
 34273       /* This row is in a right to left paragraph.  Scan it right to
 34274          left.  */
 34275       struct glyph *g;
 34276 
 34277       end = r1->glyphs[TEXT_AREA] - 1;
 34278       glyph = end + r1->used[TEXT_AREA];
 34279 
 34280       /* Skip truncation glyphs at the start of the glyph row.  */
 34281       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
 34282         for (; glyph > end
 34283                && NILP (glyph->object)
 34284                && glyph->charpos < 0;
 34285              --glyph)
 34286           ;
 34287 
 34288       /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
 34289          or DISP_STRING, and the first glyph from buffer whose
 34290          position is between START_CHARPOS and END_CHARPOS.  */
 34291       for (; glyph > end
 34292              && !NILP (glyph->object)
 34293              && !EQ (glyph->object, disp_string)
 34294              && !(BUFFERP (glyph->object)
 34295                   && (glyph->charpos >= start_charpos
 34296                       && glyph->charpos < end_charpos));
 34297            --glyph)
 34298         {
 34299           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34300              are present at buffer positions between START_CHARPOS and
 34301              END_CHARPOS, or if they come from an overlay.  */
 34302           if (EQ (glyph->object, before_string))
 34303             {
 34304               pos = string_buffer_position (before_string, start_charpos);
 34305               /* If pos == 0, it means before_string came from an
 34306                  overlay, not from a buffer position.  */
 34307               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34308                 break;
 34309             }
 34310           else if (EQ (glyph->object, after_string))
 34311             {
 34312               pos = string_buffer_position (after_string, end_charpos);
 34313               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34314                 break;
 34315             }
 34316         }
 34317 
 34318       glyph++; /* first glyph to the right of the highlighted area */
 34319       for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
 34320         x += g->pixel_width;
 34321       hlinfo->mouse_face_beg_x = x;
 34322       hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
 34323     }
 34324 
 34325   /* If the highlight ends in a different row, compute GLYPH and END
 34326      for the end row.  Otherwise, reuse the values computed above for
 34327      the row where the highlight begins.  */
 34328   if (r2 != r1)
 34329     {
 34330       if (!r2->reversed_p)
 34331         {
 34332           glyph = r2->glyphs[TEXT_AREA];
 34333           end = glyph + r2->used[TEXT_AREA];
 34334           x = r2->x;
 34335         }
 34336       else
 34337         {
 34338           end = r2->glyphs[TEXT_AREA] - 1;
 34339           glyph = end + r2->used[TEXT_AREA];
 34340         }
 34341     }
 34342 
 34343   if (!r2->reversed_p)
 34344     {
 34345       /* Skip truncation and continuation glyphs near the end of the
 34346          row, and also blanks and stretch glyphs inserted by
 34347          extend_face_to_end_of_line.  */
 34348       while (end > glyph
 34349              && NILP ((end - 1)->object))
 34350         --end;
 34351       /* Scan the rest of the glyph row from the end, looking for the
 34352          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34353          DISP_STRING, or whose position is between START_CHARPOS
 34354          and END_CHARPOS */
 34355       for (--end;
 34356              end > glyph
 34357              && !NILP (end->object)
 34358              && !EQ (end->object, disp_string)
 34359              && !(BUFFERP (end->object)
 34360                   && (end->charpos >= start_charpos
 34361                       && end->charpos < end_charpos));
 34362            --end)
 34363         {
 34364           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34365              are present at buffer positions between START_CHARPOS and
 34366              END_CHARPOS, or if they come from an overlay.  */
 34367           if (EQ (end->object, before_string))
 34368             {
 34369               pos = string_buffer_position (before_string, start_charpos);
 34370               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34371                 break;
 34372             }
 34373           else if (EQ (end->object, after_string))
 34374             {
 34375               pos = string_buffer_position (after_string, end_charpos);
 34376               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34377                 break;
 34378             }
 34379         }
 34380       /* Find the X coordinate of the last glyph to be highlighted.  */
 34381       for (; glyph <= end; ++glyph)
 34382         x += glyph->pixel_width;
 34383 
 34384       hlinfo->mouse_face_end_x = x;
 34385       hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
 34386     }
 34387   else
 34388     {
 34389       /* Skip truncation and continuation glyphs near the end of the
 34390          row, and also blanks and stretch glyphs inserted by
 34391          extend_face_to_end_of_line.  */
 34392       x = r2->x;
 34393       end++;
 34394       while (end < glyph
 34395              && NILP (end->object))
 34396         {
 34397           x += end->pixel_width;
 34398           ++end;
 34399         }
 34400       /* Scan the rest of the glyph row from the end, looking for the
 34401          first glyph that comes from BEFORE_STRING, AFTER_STRING, or
 34402          DISP_STRING, or whose position is between START_CHARPOS
 34403          and END_CHARPOS */
 34404       for ( ;
 34405              end < glyph
 34406              && !NILP (end->object)
 34407              && !EQ (end->object, disp_string)
 34408              && !(BUFFERP (end->object)
 34409                   && (end->charpos >= start_charpos
 34410                       && end->charpos < end_charpos));
 34411            ++end)
 34412         {
 34413           /* BEFORE_STRING or AFTER_STRING are only relevant if they
 34414              are present at buffer positions between START_CHARPOS and
 34415              END_CHARPOS, or if they come from an overlay.  */
 34416           if (EQ (end->object, before_string))
 34417             {
 34418               pos = string_buffer_position (before_string, start_charpos);
 34419               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34420                 break;
 34421             }
 34422           else if (EQ (end->object, after_string))
 34423             {
 34424               pos = string_buffer_position (after_string, end_charpos);
 34425               if (!pos || (pos >= start_charpos && pos < end_charpos))
 34426                 break;
 34427             }
 34428           x += end->pixel_width;
 34429         }
 34430       /* If we exited the above loop because we arrived at the last
 34431          glyph of the row, and its buffer position is still not in
 34432          range, it means the last character in range is the preceding
 34433          newline.  Bump the end column and x values to get past the
 34434          last glyph.  */
 34435       if (end == glyph
 34436           && BUFFERP (end->object)
 34437           && (end->charpos < start_charpos
 34438               || end->charpos >= end_charpos))
 34439         {
 34440           x += end->pixel_width;
 34441           ++end;
 34442         }
 34443       hlinfo->mouse_face_end_x = x;
 34444       hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
 34445     }
 34446 
 34447   hlinfo->mouse_face_window = window;
 34448   hlinfo->mouse_face_face_id
 34449     = face_at_buffer_position (w, mouse_charpos, &ignore,
 34450                                mouse_charpos + 1,
 34451                                !hlinfo->mouse_face_hidden, -1, 0);
 34452   show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 34453 }
 34454 
 34455 /* The following function is not used anymore (replaced with
 34456    mouse_face_from_string_pos), but I leave it here for the time
 34457    being, in case someone would.  */
 34458 
 34459 #if false       /* not used */
 34460 
 34461 /* Find the position of the glyph for position POS in OBJECT in
 34462    window W's current matrix, and return in *X, *Y the pixel
 34463    coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
 34464 
 34465    RIGHT_P means return the position of the right edge of the glyph.
 34466    !RIGHT_P means return the left edge position.
 34467 
 34468    If no glyph for POS exists in the matrix, return the position of
 34469    the glyph with the next smaller position that is in the matrix, if
 34470    RIGHT_P is false.  If RIGHT_P, and no glyph for POS
 34471    exists in the matrix, return the position of the glyph with the
 34472    next larger position in OBJECT.
 34473 
 34474    Value is true if a glyph was found.  */
 34475 
 34476 static bool
 34477 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
 34478                       int *hpos, int *vpos, int *x, int *y, bool right_p)
 34479 {
 34480   int yb = window_text_bottom_y (w);
 34481   struct glyph_row *r;
 34482   struct glyph *best_glyph = NULL;
 34483   struct glyph_row *best_row = NULL;
 34484   int best_x = 0;
 34485 
 34486   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34487        r->enabled_p && r->y < yb;
 34488        ++r)
 34489     {
 34490       struct glyph *g = r->glyphs[TEXT_AREA];
 34491       struct glyph *e = g + r->used[TEXT_AREA];
 34492       int gx;
 34493 
 34494       for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34495         if (EQ (g->object, object))
 34496           {
 34497             if (g->charpos == pos)
 34498               {
 34499                 best_glyph = g;
 34500                 best_x = gx;
 34501                 best_row = r;
 34502                 goto found;
 34503               }
 34504             else if (best_glyph == NULL
 34505                      || ((eabs (g->charpos - pos)
 34506                          < eabs (best_glyph->charpos - pos))
 34507                          && (right_p
 34508                              ? g->charpos < pos
 34509                              : g->charpos > pos)))
 34510               {
 34511                 best_glyph = g;
 34512                 best_x = gx;
 34513                 best_row = r;
 34514               }
 34515           }
 34516     }
 34517 
 34518  found:
 34519 
 34520   if (best_glyph)
 34521     {
 34522       *x = best_x;
 34523       *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
 34524 
 34525       if (right_p)
 34526         {
 34527           *x += best_glyph->pixel_width;
 34528           ++*hpos;
 34529         }
 34530 
 34531       *y = best_row->y;
 34532       *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
 34533     }
 34534 
 34535   return best_glyph != NULL;
 34536 }
 34537 #endif  /* not used */
 34538 
 34539 /* Find the positions of the first and the last glyphs in window W's
 34540    current matrix that occlude positions [STARTPOS..ENDPOS) in OBJECT
 34541    (assumed to be a string), and return in HLINFO's mouse_face_*
 34542    members the pixel and column/row coordinates of those glyphs.  */
 34543 
 34544 static void
 34545 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
 34546                             Lisp_Object object,
 34547                             ptrdiff_t startpos, ptrdiff_t endpos)
 34548 {
 34549   int yb = window_text_bottom_y (w);
 34550   struct glyph_row *r;
 34551   struct glyph *g, *e;
 34552   int gx;
 34553   bool found = false;
 34554 
 34555   /* Find the glyph row with at least one position in the range
 34556      [STARTPOS..ENDPOS), and the first glyph in that row whose
 34557      position belongs to that range.  */
 34558   for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
 34559        r->enabled_p && r->y < yb;
 34560        ++r)
 34561     {
 34562       if (!r->reversed_p)
 34563         {
 34564           g = r->glyphs[TEXT_AREA];
 34565           e = g + r->used[TEXT_AREA];
 34566           for (gx = r->x; g < e; gx += g->pixel_width, ++g)
 34567             if (EQ (g->object, object)
 34568                 && startpos <= g->charpos && g->charpos < endpos)
 34569               {
 34570                 hlinfo->mouse_face_beg_row
 34571                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34572                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34573                 hlinfo->mouse_face_beg_x = gx;
 34574                 found = true;
 34575                 break;
 34576               }
 34577         }
 34578       else
 34579         {
 34580           struct glyph *g1;
 34581 
 34582           e = r->glyphs[TEXT_AREA];
 34583           g = e + r->used[TEXT_AREA];
 34584           for ( ; g > e; --g)
 34585             if (EQ ((g-1)->object, object)
 34586                 && startpos <= (g-1)->charpos && (g-1)->charpos < endpos)
 34587               {
 34588                 hlinfo->mouse_face_beg_row
 34589                   = MATRIX_ROW_VPOS (r, w->current_matrix);
 34590                 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
 34591                 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
 34592                   gx += g1->pixel_width;
 34593                 hlinfo->mouse_face_beg_x = gx;
 34594                 found = true;
 34595                 break;
 34596               }
 34597         }
 34598       if (found)
 34599         break;
 34600     }
 34601 
 34602   if (!found)
 34603     return;
 34604 
 34605   /* Starting with the next row, look for the first row which does NOT
 34606      include any glyphs whose positions are in the range.  */
 34607   for (++r; r->enabled_p && r->y < yb; ++r)
 34608     {
 34609       g = r->glyphs[TEXT_AREA];
 34610       e = g + r->used[TEXT_AREA];
 34611       found = false;
 34612       for ( ; g < e; ++g)
 34613         if (EQ (g->object, object)
 34614             && startpos <= g->charpos && g->charpos < endpos)
 34615           {
 34616             found = true;
 34617             break;
 34618           }
 34619       if (!found)
 34620         break;
 34621     }
 34622 
 34623   /* The highlighted region ends on the previous row.  */
 34624   r--;
 34625 
 34626   /* Set the end row.  */
 34627   hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
 34628 
 34629   /* Compute and set the end column and the end column's horizontal
 34630      pixel coordinate.  */
 34631   if (!r->reversed_p)
 34632     {
 34633       g = r->glyphs[TEXT_AREA];
 34634       e = g + r->used[TEXT_AREA];
 34635       for ( ; e > g; --e)
 34636         if (EQ ((e-1)->object, object)
 34637             && startpos <= (e-1)->charpos && (e-1)->charpos < endpos)
 34638           break;
 34639       hlinfo->mouse_face_end_col = e - g;
 34640 
 34641       for (gx = r->x; g < e; ++g)
 34642         gx += g->pixel_width;
 34643       hlinfo->mouse_face_end_x = gx;
 34644     }
 34645   else
 34646     {
 34647       e = r->glyphs[TEXT_AREA];
 34648       g = e + r->used[TEXT_AREA];
 34649       for (gx = r->x ; e < g; ++e)
 34650         {
 34651           if (EQ (e->object, object)
 34652               && startpos <= e->charpos && e->charpos < endpos)
 34653             break;
 34654           gx += e->pixel_width;
 34655         }
 34656       hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
 34657       hlinfo->mouse_face_end_x = gx;
 34658     }
 34659 }
 34660 
 34661 #ifdef HAVE_WINDOW_SYSTEM
 34662 
 34663 /* See if position X, Y is within a hot-spot of an image.  */
 34664 
 34665 static bool
 34666 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
 34667 {
 34668   if (!CONSP (hot_spot))
 34669     return false;
 34670 
 34671   if (EQ (XCAR (hot_spot), Qrect))
 34672     {
 34673       /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1))  */
 34674       Lisp_Object rect = XCDR (hot_spot);
 34675       Lisp_Object tem;
 34676       if (!CONSP (rect))
 34677         return false;
 34678       if (!CONSP (XCAR (rect)))
 34679         return false;
 34680       if (!CONSP (XCDR (rect)))
 34681         return false;
 34682       if (!(tem = XCAR (XCAR (rect)), FIXNUMP (tem) && x >= XFIXNUM (tem)))
 34683         return false;
 34684       if (!(tem = XCDR (XCAR (rect)), FIXNUMP (tem) && y >= XFIXNUM (tem)))
 34685         return false;
 34686       if (!(tem = XCAR (XCDR (rect)), FIXNUMP (tem) && x <= XFIXNUM (tem)))
 34687         return false;
 34688       if (!(tem = XCDR (XCDR (rect)), FIXNUMP (tem) && y <= XFIXNUM (tem)))
 34689         return false;
 34690       return true;
 34691     }
 34692   else if (EQ (XCAR (hot_spot), Qcircle))
 34693     {
 34694       /* CDR is (Center . Radius) = ((x0 . y0) . r) */
 34695       Lisp_Object circ = XCDR (hot_spot);
 34696       Lisp_Object lr, lx0, ly0;
 34697       if (CONSP (circ)
 34698           && CONSP (XCAR (circ))
 34699           && (lr = XCDR (circ), NUMBERP (lr))
 34700           && (lx0 = XCAR (XCAR (circ)), FIXNUMP (lx0))
 34701           && (ly0 = XCDR (XCAR (circ)), FIXNUMP (ly0)))
 34702         {
 34703           double r = XFLOATINT (lr);
 34704           double dx = XFIXNUM (lx0) - x;
 34705           double dy = XFIXNUM (ly0) - y;
 34706           return (dx * dx + dy * dy <= r * r);
 34707         }
 34708     }
 34709   else if (EQ (XCAR (hot_spot), Qpoly))
 34710     {
 34711       /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
 34712       if (VECTORP (XCDR (hot_spot)))
 34713         {
 34714           struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
 34715           Lisp_Object *poly = v->contents;
 34716           ptrdiff_t n = v->header.size;
 34717           ptrdiff_t i;
 34718           bool inside = false;
 34719           Lisp_Object lx, ly;
 34720           int x0, y0;
 34721 
 34722           /* Need an even number of coordinates, and at least 3 edges.  */
 34723           if (n < 6 || n & 1)
 34724             return false;
 34725 
 34726           /* Count edge segments intersecting line from (X,Y) to (X,infinity).
 34727              If count is odd, we are inside polygon.  Pixels on edges
 34728              may or may not be included depending on actual geometry of the
 34729              polygon.  */
 34730           if ((lx = poly[n-2], !FIXNUMP (lx))
 34731               || (ly = poly[n-1], !FIXNUMP (lx)))
 34732             return false;
 34733           x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34734           for (i = 0; i < n; i += 2)
 34735             {
 34736               int x1 = x0, y1 = y0;
 34737               if ((lx = poly[i], !FIXNUMP (lx))
 34738                   || (ly = poly[i+1], !FIXNUMP (ly)))
 34739                 return false;
 34740               x0 = XFIXNUM (lx), y0 = XFIXNUM (ly);
 34741 
 34742               /* Does this segment cross the X line?  */
 34743               if (x0 >= x)
 34744                 {
 34745                   if (x1 >= x)
 34746                     continue;
 34747                 }
 34748               else if (x1 < x)
 34749                 continue;
 34750               if (y > y0 && y > y1)
 34751                 continue;
 34752               if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
 34753                 inside = !inside;
 34754             }
 34755           return inside;
 34756         }
 34757     }
 34758   return false;
 34759 }
 34760 
 34761 Lisp_Object
 34762 find_hot_spot (Lisp_Object map, int x, int y)
 34763 {
 34764   while (CONSP (map))
 34765     {
 34766       if (CONSP (XCAR (map))
 34767           && on_hot_spot_p (XCAR (XCAR (map)), x, y))
 34768         return XCAR (map);
 34769       map = XCDR (map);
 34770     }
 34771 
 34772   return Qnil;
 34773 }
 34774 
 34775 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
 34776        3, 3, 0,
 34777        doc: /* Lookup in image map MAP coordinates X and Y.
 34778 An image map is an alist where each element has the format (AREA ID PLIST).
 34779 An AREA is specified as either a rectangle, a circle, or a polygon:
 34780 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
 34781 pixel coordinates of the upper left and bottom right corners.
 34782 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
 34783 and the radius of the circle; r may be a float or integer.
 34784 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
 34785 vector describes one corner in the polygon.
 34786 Returns the alist element for the first matching AREA in MAP.  */)
 34787   (Lisp_Object map, Lisp_Object x, Lisp_Object y)
 34788 {
 34789   if (NILP (map))
 34790     return Qnil;
 34791 
 34792   CHECK_FIXNUM (x);
 34793   CHECK_FIXNUM (y);
 34794 
 34795   return find_hot_spot (map,
 34796                         clip_to_bounds (INT_MIN, XFIXNUM (x), INT_MAX),
 34797                         clip_to_bounds (INT_MIN, XFIXNUM (y), INT_MAX));
 34798 }
 34799 #endif  /* HAVE_WINDOW_SYSTEM */
 34800 
 34801 
 34802 /* Display frame CURSOR, optionally using shape defined by POINTER.  */
 34803 static void
 34804 define_frame_cursor1 (struct frame *f, Emacs_Cursor cursor, Lisp_Object pointer)
 34805 {
 34806 #ifdef HAVE_WINDOW_SYSTEM
 34807   if (!FRAME_WINDOW_P (f))
 34808     return;
 34809 
 34810   /* Do not change cursor shape while dragging mouse.  */
 34811   if (EQ (track_mouse, Qdragging) || EQ (track_mouse, Qdropping)
 34812       || EQ (track_mouse, Qdrag_source))
 34813     return;
 34814 
 34815   if (!NILP (pointer))
 34816     {
 34817       if (EQ (pointer, Qarrow))
 34818         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34819       else if (EQ (pointer, Qhand))
 34820         cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 34821       else if (EQ (pointer, Qtext))
 34822         cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 34823       else if (EQ (pointer, intern ("hdrag")))
 34824         cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 34825       else if (EQ (pointer, intern ("nhdrag")))
 34826         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 34827 # ifdef HAVE_X_WINDOWS
 34828       else if (EQ (pointer, intern ("vdrag")))
 34829         cursor = FRAME_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
 34830 # endif
 34831       else if (EQ (pointer, intern ("hourglass")))
 34832         cursor = FRAME_OUTPUT_DATA (f)->hourglass_cursor;
 34833       else if (EQ (pointer, Qmodeline))
 34834         cursor = FRAME_OUTPUT_DATA (f)->modeline_cursor;
 34835       else
 34836         cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34837     }
 34838 
 34839   if (cursor != No_Cursor)
 34840     FRAME_RIF (f)->define_frame_cursor (f, cursor);
 34841 #endif
 34842 }
 34843 
 34844 /* Take proper action when mouse has moved to the mode or header line
 34845    or marginal area AREA of window W, x-position X and y-position Y.
 34846    X is relative to the start of the text display area of W, so the
 34847    width of bitmap areas and scroll bars must be subtracted to get a
 34848    position relative to the start of the mode line.  */
 34849 
 34850 static void
 34851 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
 34852                                     enum window_part area)
 34853 {
 34854   struct window *w = XWINDOW (window);
 34855   struct frame *f = XFRAME (w->frame);
 34856   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 34857   Emacs_Cursor cursor = No_Cursor;
 34858   Lisp_Object pointer = Qnil;
 34859   int dx, dy, width, height;
 34860   ptrdiff_t charpos;
 34861   Lisp_Object string, object = Qnil;
 34862   Lisp_Object pos UNINIT;
 34863   Lisp_Object mouse_face;
 34864   int original_x_pixel = x;
 34865   struct glyph * glyph = NULL, * row_start_glyph = NULL;
 34866   struct glyph_row *row UNINIT;
 34867 
 34868   if (area == ON_MODE_LINE || area == ON_HEADER_LINE || area == ON_TAB_LINE)
 34869     {
 34870       int x0;
 34871       struct glyph *end;
 34872 
 34873       /* Kludge alert: mode_line_string takes X/Y in pixels, but
 34874          returns them in row/column units!  */
 34875       string = mode_line_string (w, area, &x, &y, &charpos,
 34876                                  &object, &dx, &dy, &width, &height);
 34877 
 34878       row = (area == ON_MODE_LINE
 34879              ? MATRIX_MODE_LINE_ROW (w->current_matrix)
 34880              : (area == ON_TAB_LINE
 34881                 ? MATRIX_TAB_LINE_ROW (w->current_matrix)
 34882                 : MATRIX_HEADER_LINE_ROW (w->current_matrix)));
 34883 
 34884       /* Find the glyph under the mouse pointer.  */
 34885       if (row->mode_line_p && row->enabled_p)
 34886         {
 34887           glyph = row_start_glyph = row->glyphs[TEXT_AREA];
 34888           end = glyph + row->used[TEXT_AREA];
 34889 
 34890           for (x0 = original_x_pixel;
 34891                glyph < end && x0 >= glyph->pixel_width;
 34892                ++glyph)
 34893             x0 -= glyph->pixel_width;
 34894 
 34895           if (glyph >= end)
 34896             glyph = NULL;
 34897         }
 34898     }
 34899   else
 34900     {
 34901       x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
 34902       /* Kludge alert: marginal_area_string takes X/Y in pixels, but
 34903          returns them in row/column units!  */
 34904       string = marginal_area_string (w, area, &x, &y, &charpos,
 34905                                      &object, &dx, &dy, &width, &height);
 34906     }
 34907 
 34908   Lisp_Object help = Qnil;
 34909 
 34910 #ifdef HAVE_WINDOW_SYSTEM
 34911   if (IMAGEP (object))
 34912     {
 34913       Lisp_Object image_map, hotspot;
 34914       if ((image_map = plist_get (XCDR (object), QCmap),
 34915            !NILP (image_map))
 34916           && (hotspot = find_hot_spot (image_map, dx, dy),
 34917               CONSP (hotspot))
 34918           && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 34919         {
 34920           Lisp_Object plist;
 34921 
 34922           /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
 34923              If so, we could look for mouse-enter, mouse-leave
 34924              properties in PLIST (and do something...).  */
 34925           hotspot = XCDR (hotspot);
 34926           if (CONSP (hotspot)
 34927               && (plist = XCAR (hotspot), CONSP (plist)))
 34928             {
 34929               pointer = plist_get (plist, Qpointer);
 34930               if (NILP (pointer))
 34931                 pointer = Qhand;
 34932               help = plist_get (plist, Qhelp_echo);
 34933               if (!NILP (help))
 34934                 {
 34935                   help_echo_string = help;
 34936                   XSETWINDOW (help_echo_window, w);
 34937                   help_echo_object = w->contents;
 34938                   help_echo_pos = charpos;
 34939                 }
 34940             }
 34941         }
 34942       if (NILP (pointer))
 34943         pointer = plist_get (XCDR (object), QCpointer);
 34944     }
 34945 #endif  /* HAVE_WINDOW_SYSTEM */
 34946 
 34947   /* CHARPOS can be beyond the last position of STRING due, e.g., to
 34948      min-width 'display' property.  Fix that, to let all the calls to
 34949      get-text-property below do their thing.  */
 34950   if (STRINGP (string))
 34951     pos = make_fixnum (min (charpos, SCHARS (string) - 1));
 34952 
 34953   /* Set the help text and mouse pointer.  If the mouse is on a part
 34954      of the mode line without any text (e.g. past the right edge of
 34955      the mode line text), use that windows's mode line help echo if it
 34956      has been set.  */
 34957   if (STRINGP (string) || area == ON_MODE_LINE || area == ON_HEADER_LINE
 34958       || area == ON_TAB_LINE)
 34959     {
 34960       /* Arrange to display the help by setting the global variables
 34961          help_echo_string, help_echo_object, and help_echo_pos.  */
 34962       if (NILP (help))
 34963         {
 34964           if (STRINGP (string))
 34965             help = Fget_text_property (pos, Qhelp_echo, string);
 34966 
 34967           if (!NILP (help))
 34968             {
 34969               help_echo_string = help;
 34970               XSETWINDOW (help_echo_window, w);
 34971               help_echo_object = string;
 34972               help_echo_pos = charpos;
 34973             }
 34974           else if (area == ON_MODE_LINE
 34975                    && !NILP (w->mode_line_help_echo))
 34976             {
 34977               help_echo_string =  w->mode_line_help_echo;
 34978               XSETWINDOW (help_echo_window, w);
 34979               help_echo_object = Qnil;
 34980               help_echo_pos = -1;
 34981             }
 34982         }
 34983 
 34984 #ifdef HAVE_WINDOW_SYSTEM
 34985       /* Change the mouse pointer according to what is under it.  */
 34986       if (FRAME_WINDOW_P (f))
 34987         {
 34988           bool draggable = (! WINDOW_BOTTOMMOST_P (w)
 34989                             || minibuf_level
 34990                             || NILP (Vresize_mini_windows));
 34991 
 34992           if (STRINGP (string))
 34993             {
 34994               cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 34995 
 34996               if (NILP (pointer))
 34997                 pointer = Fget_text_property (pos, Qpointer, string);
 34998 
 34999               /* Change the mouse pointer according to what is under X/Y.  */
 35000               if (NILP (pointer)
 35001                   && (area == ON_MODE_LINE || area == ON_HEADER_LINE
 35002                       || area == ON_TAB_LINE))
 35003                 {
 35004                   Lisp_Object map;
 35005 
 35006                   map = Fget_text_property (pos, Qlocal_map, string);
 35007                   if (!KEYMAPP (map))
 35008                     map = Fget_text_property (pos, Qkeymap, string);
 35009                   if (!KEYMAPP (map) && draggable && area == ON_MODE_LINE)
 35010                     cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35011                 }
 35012             }
 35013           else if (draggable && area == ON_MODE_LINE)
 35014             cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35015           else if ((area == ON_MODE_LINE
 35016                     && WINDOW_BOTTOMMOST_P (w)
 35017                     && !FRAME_HAS_MINIBUF_P (f)
 35018                     && !NILP (Fframe_parameter
 35019                               (w->frame, Qdrag_with_mode_line)))
 35020                    || (((area == ON_HEADER_LINE
 35021                          && !NILP (Fframe_parameter
 35022                                    (w->frame, Qdrag_with_header_line)))
 35023                         || (area == ON_TAB_LINE
 35024                             && !NILP (Fframe_parameter
 35025                                       (w->frame, Qdrag_with_tab_line))))
 35026                        && WINDOW_TOPMOST_P (w)))
 35027             cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35028           else
 35029             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35030         }
 35031 #endif
 35032     }
 35033 
 35034   /* Change the mouse face according to what is under X/Y.  */
 35035   bool mouse_face_shown = false;
 35036 
 35037   if (STRINGP (string))
 35038     {
 35039       mouse_face = Fget_text_property (pos, Qmouse_face, string);
 35040       if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
 35041           && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)
 35042               || (area == ON_TAB_LINE))
 35043           && glyph)
 35044         {
 35045           Lisp_Object b, e;
 35046 
 35047           struct glyph * tmp_glyph;
 35048 
 35049           int gpos;
 35050           int gseq_length;
 35051           int total_pixel_width;
 35052           ptrdiff_t begpos, endpos, ignore;
 35053 
 35054           int vpos, hpos;
 35055 
 35056           b = Fprevious_single_property_change (make_fixnum (charpos + 1),
 35057                                                 Qmouse_face, string, Qnil);
 35058           if (NILP (b))
 35059             begpos = 0;
 35060           else
 35061             begpos = XFIXNUM (b);
 35062 
 35063           e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
 35064           if (NILP (e))
 35065             endpos = SCHARS (string);
 35066           else
 35067             endpos = XFIXNUM (e);
 35068 
 35069           /* Calculate the glyph position GPOS of GLYPH in the
 35070              displayed string, relative to the beginning of the
 35071              highlighted part of the string.
 35072 
 35073              Note: GPOS is different from CHARPOS.  CHARPOS is the
 35074              position of GLYPH in the internal string object.  A mode
 35075              line string format has structures which are converted to
 35076              a flattened string by the Emacs Lisp interpreter.  The
 35077              internal string is an element of those structures.  The
 35078              displayed string is the flattened string.  */
 35079           tmp_glyph = row_start_glyph;
 35080           while (tmp_glyph < glyph
 35081                  && (!(EQ (tmp_glyph->object, glyph->object)
 35082                        && begpos <= tmp_glyph->charpos
 35083                        && tmp_glyph->charpos < endpos)))
 35084             tmp_glyph++;
 35085           gpos = glyph - tmp_glyph;
 35086 
 35087           /* Calculate the length GSEQ_LENGTH of the glyph sequence of
 35088              the highlighted part of the displayed string to which
 35089              GLYPH belongs.  Note: GSEQ_LENGTH is different from
 35090              SCHARS (STRING), because the latter returns the length of
 35091              the internal string.  */
 35092           for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
 35093                tmp_glyph > glyph
 35094                  && (!(EQ (tmp_glyph->object, glyph->object)
 35095                        && begpos <= tmp_glyph->charpos
 35096                        && tmp_glyph->charpos < endpos));
 35097                tmp_glyph--)
 35098             ;
 35099           gseq_length = gpos + (tmp_glyph - glyph) + 1;
 35100 
 35101           /* Calculate the total pixel width of all the glyphs between
 35102              the beginning of the highlighted area and GLYPH.  */
 35103           total_pixel_width = 0;
 35104           for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
 35105             total_pixel_width += tmp_glyph->pixel_width;
 35106 
 35107           /* Pre calculation of re-rendering position.  Note: X is in
 35108              column units here, after the call to mode_line_string or
 35109              marginal_area_string.  */
 35110           hpos = x - gpos;
 35111           vpos = (area == ON_MODE_LINE
 35112                   ? (w->current_matrix)->nrows - 1
 35113                   : (area == ON_TAB_LINE
 35114                      ? 0
 35115                      : (w->current_matrix->tab_line_p
 35116                         ? 1
 35117                         : 0)));
 35118 
 35119           /* If GLYPH's position is included in the region that is
 35120              already drawn in mouse face, we have nothing to do.  */
 35121           if ( EQ (window, hlinfo->mouse_face_window)
 35122                && (!row->reversed_p
 35123                    ? (hlinfo->mouse_face_beg_col <= hpos
 35124                       && hpos < hlinfo->mouse_face_end_col)
 35125                    /* In R2L rows we swap BEG and END, see below.  */
 35126                    : (hlinfo->mouse_face_end_col <= hpos
 35127                       && hpos < hlinfo->mouse_face_beg_col))
 35128                && hlinfo->mouse_face_beg_row == vpos )
 35129             return;
 35130 
 35131           if (clear_mouse_face (hlinfo))
 35132             cursor = No_Cursor;
 35133 
 35134           if (!row->reversed_p)
 35135             {
 35136               hlinfo->mouse_face_beg_col = hpos;
 35137               hlinfo->mouse_face_beg_x   = original_x_pixel
 35138                                             - (total_pixel_width + dx);
 35139               hlinfo->mouse_face_end_col = hpos + gseq_length;
 35140               hlinfo->mouse_face_end_x   = 0;
 35141             }
 35142           else
 35143             {
 35144               /* In R2L rows, show_mouse_face expects BEG and END
 35145                  coordinates to be swapped.  */
 35146               hlinfo->mouse_face_end_col = hpos;
 35147               hlinfo->mouse_face_end_x   = original_x_pixel
 35148                                             - (total_pixel_width + dx);
 35149               hlinfo->mouse_face_beg_col = hpos + gseq_length;
 35150               hlinfo->mouse_face_beg_x   = 0;
 35151             }
 35152 
 35153           hlinfo->mouse_face_beg_row  = vpos;
 35154           hlinfo->mouse_face_end_row  = hlinfo->mouse_face_beg_row;
 35155           hlinfo->mouse_face_past_end = false;
 35156           hlinfo->mouse_face_window   = window;
 35157 
 35158           hlinfo->mouse_face_face_id =
 35159             face_at_string_position (w, string, charpos, 0, &ignore,
 35160                                      glyph->face_id, true, 0);
 35161 
 35162           show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35163           mouse_face_shown = true;
 35164 
 35165           if (NILP (pointer))
 35166             pointer = Qhand;
 35167         }
 35168     }
 35169 
 35170   /* If mouse-face doesn't need to be shown, clear any existing
 35171      mouse-face.  */
 35172   if ((area == ON_MODE_LINE || area == ON_HEADER_LINE
 35173        || area == ON_TAB_LINE) && !mouse_face_shown)
 35174     clear_mouse_face (hlinfo);
 35175 
 35176   define_frame_cursor1 (f, cursor, pointer);
 35177 }
 35178 
 35179 
 35180 /* EXPORT:
 35181    Take proper action when the mouse has moved to position X, Y on
 35182    frame F with regards to highlighting portions of display that have
 35183    mouse-face properties.  Also de-highlight portions of display where
 35184    the mouse was before, set the mouse pointer shape as appropriate
 35185    for the mouse coordinates, and activate help echo (tooltips).
 35186    X and Y can be negative or out of range.  */
 35187 
 35188 void
 35189 note_mouse_highlight (struct frame *f, int x, int y)
 35190 {
 35191   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35192   enum window_part part = ON_NOTHING;
 35193   Lisp_Object window;
 35194   struct window *w;
 35195   Emacs_Cursor cursor = No_Cursor;
 35196   Lisp_Object pointer = Qnil;  /* Takes precedence over cursor!  */
 35197   struct buffer *b;
 35198 
 35199   /* When a menu is active, don't highlight because this looks odd.  */
 35200 #if defined (HAVE_X_WINDOWS) || defined (HAVE_NS) || defined (MSDOS)
 35201   if (popup_activated ())
 35202     return;
 35203 #endif
 35204 
 35205 #if defined (HAVE_HAIKU)
 35206   if (popup_activated_p)
 35207     return;
 35208 #endif
 35209 
 35210   if (!f->glyphs_initialized_p
 35211       || f->pointer_invisible)
 35212     return;
 35213 
 35214   hlinfo->mouse_face_mouse_x = x;
 35215   hlinfo->mouse_face_mouse_y = y;
 35216   hlinfo->mouse_face_mouse_frame = f;
 35217 
 35218   if (hlinfo->mouse_face_defer)
 35219     return;
 35220 
 35221   /* Which window is that in?  */
 35222   window = window_from_coordinates (f, x, y, &part, true, true);
 35223 
 35224   /* If displaying active text in another window, clear that.  */
 35225   if (! EQ (window, hlinfo->mouse_face_window)
 35226       /* Also clear if we move out of text area in same window.  */
 35227       || (!NILP (hlinfo->mouse_face_window)
 35228           && !NILP (window)
 35229           && part != ON_TEXT
 35230           && part != ON_MODE_LINE
 35231           && part != ON_HEADER_LINE
 35232           && part != ON_TAB_LINE))
 35233     clear_mouse_face (hlinfo);
 35234 
 35235   /* Reset help_echo_string.  It will get recomputed below.  */
 35236   help_echo_string = Qnil;
 35237 
 35238   /* Handle tab-bar highlight on mouse-capable TTY frames.  */
 35239   if (!FRAME_WINDOW_P (f)
 35240       && (y >= FRAME_MENU_BAR_LINES (f)
 35241           && y < FRAME_MENU_BAR_LINES (f) + FRAME_TAB_BAR_LINES (f)))
 35242     {
 35243       int prop_idx;
 35244       bool ignore;
 35245       Lisp_Object caption = tty_get_tab_bar_item (f, x, &prop_idx, &ignore);
 35246 
 35247       if (!NILP (caption))
 35248         {
 35249           help_echo_object = help_echo_window = Qnil;
 35250           help_echo_pos = -1;
 35251           help_echo_string = AREF (f->tab_bar_items,
 35252                                    prop_idx * TAB_BAR_ITEM_NSLOTS
 35253                                    + TAB_BAR_ITEM_HELP);
 35254           if (NILP (help_echo_string))
 35255             help_echo_string = caption;
 35256         }
 35257     }
 35258 
 35259 #ifdef HAVE_WINDOW_SYSTEM
 35260   /* If the cursor is on the internal border of FRAME and FRAME's
 35261      internal border is draggable, provide some visual feedback.  */
 35262   if (FRAME_INTERNAL_BORDER_WIDTH (f) > 0
 35263       && !NILP (get_frame_param (f, Qdrag_internal_border)))
 35264     {
 35265       enum internal_border_part part = frame_internal_border_part (f, x, y);
 35266 
 35267       switch (part)
 35268         {
 35269         case INTERNAL_BORDER_NONE:
 35270           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35271             /* Reset cursor.  */
 35272             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35273           break;
 35274         case INTERNAL_BORDER_LEFT_EDGE:
 35275           cursor = FRAME_OUTPUT_DATA (f)->left_edge_cursor;
 35276           break;
 35277         case INTERNAL_BORDER_TOP_LEFT_CORNER:
 35278           cursor = FRAME_OUTPUT_DATA (f)->top_left_corner_cursor;
 35279           break;
 35280         case INTERNAL_BORDER_TOP_EDGE:
 35281           cursor = FRAME_OUTPUT_DATA (f)->top_edge_cursor;
 35282           break;
 35283         case INTERNAL_BORDER_TOP_RIGHT_CORNER:
 35284           cursor = FRAME_OUTPUT_DATA (f)->top_right_corner_cursor;
 35285           break;
 35286         case INTERNAL_BORDER_RIGHT_EDGE:
 35287           cursor = FRAME_OUTPUT_DATA (f)->right_edge_cursor;
 35288           break;
 35289         case INTERNAL_BORDER_BOTTOM_RIGHT_CORNER:
 35290           cursor = FRAME_OUTPUT_DATA (f)->bottom_right_corner_cursor;
 35291           break;
 35292         case INTERNAL_BORDER_BOTTOM_EDGE:
 35293           cursor = FRAME_OUTPUT_DATA (f)->bottom_edge_cursor;
 35294           break;
 35295         case INTERNAL_BORDER_BOTTOM_LEFT_CORNER:
 35296           cursor = FRAME_OUTPUT_DATA (f)->bottom_left_corner_cursor;
 35297           break;
 35298         default:
 35299           /* This should not happen.  */
 35300           if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35301             cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35302         }
 35303 
 35304       if (cursor != FRAME_OUTPUT_DATA (f)->nontext_cursor)
 35305         {
 35306           /* Do we really want a help echo here?  */
 35307           help_echo_string = build_string ("drag-mouse-1: resize frame");
 35308           goto set_cursor;
 35309         }
 35310     }
 35311 #endif /* HAVE_WINDOW_SYSTEM */
 35312 
 35313   /* Not on a window -> return.  */
 35314   if (!WINDOWP (window))
 35315     return;
 35316 
 35317   /* Convert to window-relative pixel coordinates.  */
 35318   w = XWINDOW (window);
 35319   frame_to_window_pixel_xy (w, &x, &y);
 35320 
 35321 #if defined (HAVE_WINDOW_SYSTEM)
 35322   /* Handle tab-bar window differently since it doesn't display a
 35323      buffer.  */
 35324   if (EQ (window, f->tab_bar_window))
 35325     {
 35326       note_tab_bar_highlight (f, x, y);
 35327       if (tab_bar__dragging_in_progress)
 35328         {
 35329           cursor = FRAME_OUTPUT_DATA (f)->hand_cursor;
 35330           goto set_cursor;
 35331         }
 35332       else
 35333         return;
 35334     }
 35335   else
 35336     {
 35337       /* The mouse might have pressed into the tab bar, but might
 35338          also have been released outside the tab bar, so
 35339          f->last_tab_bar_item must be reset, in order to make sure the
 35340          item can be still highlighted again in the future.  */
 35341       f->last_tab_bar_item = -1;
 35342     }
 35343 #endif
 35344 
 35345 #if defined (HAVE_WINDOW_SYSTEM) && ! defined (HAVE_EXT_TOOL_BAR)
 35346   /* Handle tool-bar window differently since it doesn't display a
 35347      buffer.  */
 35348   if (EQ (window, f->tool_bar_window))
 35349     {
 35350       note_tool_bar_highlight (f, x, y);
 35351       return;
 35352     }
 35353 #endif
 35354 
 35355   /* Mouse is on the mode, header line or margin?  */
 35356   if (part == ON_MODE_LINE || part == ON_HEADER_LINE || part == ON_TAB_LINE
 35357       || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35358     {
 35359       note_mode_line_or_margin_highlight (window, x, y, part);
 35360 
 35361 #ifdef HAVE_WINDOW_SYSTEM
 35362       if (part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
 35363         {
 35364           cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35365           /* Show non-text cursor (Bug#16647).  */
 35366           goto set_cursor;
 35367         }
 35368       else
 35369 #endif
 35370         return;
 35371     }
 35372 
 35373 #ifdef HAVE_WINDOW_SYSTEM
 35374   if (part == ON_VERTICAL_BORDER)
 35375     {
 35376       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35377       help_echo_string = build_string ("drag-mouse-1: resize");
 35378       goto set_cursor;
 35379     }
 35380   else if (part == ON_RIGHT_DIVIDER)
 35381     {
 35382       cursor = FRAME_OUTPUT_DATA (f)->horizontal_drag_cursor;
 35383       help_echo_string = build_string ("drag-mouse-1: resize");
 35384       goto set_cursor;
 35385     }
 35386   else if (part == ON_BOTTOM_DIVIDER)
 35387     if (! WINDOW_BOTTOMMOST_P (w)
 35388         || minibuf_level
 35389         || NILP (Vresize_mini_windows))
 35390       {
 35391         cursor = FRAME_OUTPUT_DATA (f)->vertical_drag_cursor;
 35392         help_echo_string = build_string ("drag-mouse-1: resize");
 35393         goto set_cursor;
 35394       }
 35395     else
 35396       cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35397   else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
 35398            || part == ON_VERTICAL_SCROLL_BAR
 35399            || part == ON_HORIZONTAL_SCROLL_BAR)
 35400     cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35401   else
 35402     cursor = FRAME_OUTPUT_DATA (f)->text_cursor;
 35403 #endif
 35404 
 35405   /* Are we in a window whose display is up to date?
 35406      And verify the buffer's text has not changed.  */
 35407   b = XBUFFER (w->contents);
 35408   if (part == ON_TEXT && w->window_end_valid && !window_outdated (w))
 35409     {
 35410       int hpos, vpos, dx, dy, area = LAST_AREA;
 35411       ptrdiff_t pos;
 35412       struct glyph *glyph;
 35413       Lisp_Object object;
 35414       Lisp_Object mouse_face = Qnil, position;
 35415       Lisp_Object *overlay_vec = NULL;
 35416       ptrdiff_t i, noverlays;
 35417       struct buffer *obuf;
 35418       ptrdiff_t obegv, ozv;
 35419       bool same_region;
 35420 
 35421       /* Find the glyph under X/Y.  */
 35422       glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
 35423 
 35424 #ifdef HAVE_WINDOW_SYSTEM
 35425       /* Look for :pointer property on image.  */
 35426       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
 35427         {
 35428           struct image *img = IMAGE_OPT_FROM_ID (f, glyph->u.img_id);
 35429           if (img != NULL && IMAGEP (img->spec))
 35430             {
 35431               Lisp_Object image_map, hotspot;
 35432               if ((image_map = plist_get (XCDR (img->spec), QCmap),
 35433                    !NILP (image_map))
 35434                   && (hotspot = find_hot_spot (image_map,
 35435                                                glyph->slice.img.x + dx,
 35436                                                glyph->slice.img.y + dy),
 35437                       CONSP (hotspot))
 35438                   && (hotspot = XCDR (hotspot), CONSP (hotspot)))
 35439                 {
 35440                   Lisp_Object plist;
 35441 
 35442                   /* Could check XCAR (hotspot) to see if we enter/leave
 35443                      this hot-spot.
 35444                      If so, we could look for mouse-enter, mouse-leave
 35445                      properties in PLIST (and do something...).  */
 35446                   hotspot = XCDR (hotspot);
 35447                   if (CONSP (hotspot)
 35448                       && (plist = XCAR (hotspot), CONSP (plist)))
 35449                     {
 35450                       pointer = plist_get (plist, Qpointer);
 35451                       if (NILP (pointer))
 35452                         pointer = Qhand;
 35453                       help_echo_string = plist_get (plist, Qhelp_echo);
 35454                       if (!NILP (help_echo_string))
 35455                         {
 35456                           help_echo_window = window;
 35457                           help_echo_object = glyph->object;
 35458                           help_echo_pos = glyph->charpos;
 35459                         }
 35460                     }
 35461                 }
 35462               if (NILP (pointer))
 35463                 pointer = plist_get (XCDR (img->spec), QCpointer);
 35464             }
 35465         }
 35466 #endif  /* HAVE_WINDOW_SYSTEM */
 35467 
 35468       /* Clear mouse face if X/Y not over text.  */
 35469       if (glyph == NULL
 35470           || area != TEXT_AREA
 35471           || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
 35472           /* Glyph's OBJECT is nil for glyphs inserted by the
 35473              display engine for its internal purposes, like truncation
 35474              and continuation glyphs and blanks beyond the end of
 35475              line's text on text terminals.  If we are over such a
 35476              glyph, we are not over any text.  */
 35477           || NILP (glyph->object)
 35478           /* R2L rows have a stretch glyph at their front, which
 35479              stands for no text, whereas L2R rows have no glyphs at
 35480              all beyond the end of text.  Treat such stretch glyphs
 35481              like we do with NULL glyphs in L2R rows.  */
 35482           || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
 35483               && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
 35484               && glyph->type == STRETCH_GLYPH
 35485               && glyph->avoid_cursor_p))
 35486         {
 35487           if (clear_mouse_face (hlinfo))
 35488             cursor = No_Cursor;
 35489           if (FRAME_WINDOW_P (f) && NILP (pointer))
 35490             {
 35491 #ifdef HAVE_WINDOW_SYSTEM
 35492               if (area != TEXT_AREA)
 35493                 cursor = FRAME_OUTPUT_DATA (f)->nontext_cursor;
 35494               else
 35495                 pointer = Vvoid_text_area_pointer;
 35496 #endif
 35497             }
 35498           goto set_cursor;
 35499         }
 35500 
 35501       pos = glyph->charpos;
 35502       object = glyph->object;
 35503       if (!STRINGP (object) && !BUFFERP (object))
 35504         goto set_cursor;
 35505 
 35506       /* If we get an out-of-range value, return now; avoid an error.  */
 35507       if (BUFFERP (object) && pos > BUF_Z (b))
 35508         goto set_cursor;
 35509 
 35510       /* Make the window's buffer temporarily current for
 35511          overlays_at and compute_char_face.  */
 35512       obuf = current_buffer;
 35513       current_buffer = b;
 35514       obegv = BEGV;
 35515       ozv = ZV;
 35516       BEGV = BEG;
 35517       ZV = Z;
 35518 
 35519       /* Is this char mouse-active or does it have help-echo?  */
 35520       position = make_fixnum (pos);
 35521 
 35522       USE_SAFE_ALLOCA;
 35523 
 35524       if (BUFFERP (object))
 35525         {
 35526           /* Put all the overlays we want in a vector in overlay_vec.  */
 35527           GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL);
 35528           /* Sort overlays into increasing priority order.  */
 35529           noverlays = sort_overlays (overlay_vec, noverlays, w);
 35530         }
 35531       else
 35532         noverlays = 0;
 35533 
 35534       if (NILP (Vmouse_highlight))
 35535         {
 35536           clear_mouse_face (hlinfo);
 35537           goto check_help_echo;
 35538         }
 35539 
 35540       same_region = coords_in_mouse_face_p (w, hpos, vpos);
 35541 
 35542       if (same_region)
 35543         cursor = No_Cursor;
 35544 
 35545       /* Check mouse-face highlighting.  */
 35546       if (! same_region
 35547           /* If there exists an overlay with mouse-face overlapping
 35548              the one we are currently highlighting, we have to check
 35549              if we enter the overlapping overlay, and then highlight
 35550              only that.  Skip the check when mouse-face highlighting
 35551              is currently hidden to avoid Bug#30519.  */
 35552           || (!hlinfo->mouse_face_hidden
 35553               && OVERLAYP (hlinfo->mouse_face_overlay)
 35554               /* It's possible the overlay was deleted (Bug#35273).  */
 35555               && OVERLAY_BUFFER (hlinfo->mouse_face_overlay)
 35556               && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
 35557         {
 35558           /* Find the highest priority overlay with a mouse-face.  */
 35559           Lisp_Object overlay = Qnil;
 35560           for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
 35561             {
 35562               mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
 35563               if (!NILP (mouse_face))
 35564                 overlay = overlay_vec[i];
 35565             }
 35566 
 35567           /* If we're highlighting the same overlay as before, there's
 35568              no need to do that again.  */
 35569           if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
 35570             goto check_help_echo;
 35571 
 35572           /* Clear the display of the old active region, if any.  */
 35573           if (clear_mouse_face (hlinfo))
 35574             cursor = No_Cursor;
 35575 
 35576           /* Record the overlay, if any, to be highlighted.  */
 35577           hlinfo->mouse_face_overlay = overlay;
 35578 
 35579           /* If no overlay applies, get a text property.  */
 35580           if (NILP (overlay))
 35581             mouse_face = Fget_text_property (position, Qmouse_face, object);
 35582 
 35583           /* Next, compute the bounds of the mouse highlighting and
 35584              display it.  */
 35585           if (!NILP (mouse_face) && STRINGP (object))
 35586             {
 35587               /* The mouse-highlighting comes from a display string
 35588                  with a mouse-face.  */
 35589               Lisp_Object s, e;
 35590               ptrdiff_t ignore;
 35591 
 35592               s = Fprevious_single_property_change
 35593                 (make_fixnum (pos + 1), Qmouse_face, object, Qnil);
 35594               e = Fnext_single_property_change
 35595                 (position, Qmouse_face, object, Qnil);
 35596               if (NILP (s))
 35597                 s = make_fixnum (0);
 35598               if (NILP (e))
 35599                 e = make_fixnum (SCHARS (object));
 35600               mouse_face_from_string_pos (w, hlinfo, object,
 35601                                           XFIXNUM (s), XFIXNUM (e));
 35602               hlinfo->mouse_face_past_end = false;
 35603               hlinfo->mouse_face_window = window;
 35604               hlinfo->mouse_face_face_id
 35605                 = face_at_string_position (w, object, pos, 0, &ignore,
 35606                                            glyph->face_id, true, 0);
 35607               show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 35608               cursor = No_Cursor;
 35609             }
 35610           else
 35611             {
 35612               /* The mouse-highlighting, if any, comes from an overlay
 35613                  or text property in the buffer.  */
 35614               Lisp_Object buffer UNINIT;
 35615               Lisp_Object disp_string UNINIT;
 35616 
 35617               if (STRINGP (object))
 35618                 {
 35619                   /* If we are on a display string with no mouse-face,
 35620                      check if the text under it has one.  */
 35621                   struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
 35622                   ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35623                   pos = string_buffer_position (object, start);
 35624                   if (pos > 0)
 35625                     {
 35626                       mouse_face = get_char_property_and_overlay
 35627                         (make_fixnum (pos), Qmouse_face, w->contents, &overlay);
 35628                       buffer = w->contents;
 35629                       disp_string = object;
 35630                     }
 35631                 }
 35632               else
 35633                 {
 35634                   buffer = object;
 35635                   disp_string = Qnil;
 35636                 }
 35637 
 35638               if (!NILP (mouse_face))
 35639                 {
 35640                   Lisp_Object before, after;
 35641                   Lisp_Object before_string, after_string;
 35642                   /* To correctly find the limits of mouse highlight
 35643                      in a bidi-reordered buffer, we must not use the
 35644                      optimization of limiting the search in
 35645                      previous-single-property-change and
 35646                      next-single-property-change, because
 35647                      rows_from_pos_range needs the real start and end
 35648                      positions to DTRT in this case.  That's because
 35649                      the first row visible in a window does not
 35650                      necessarily display the character whose position
 35651                      is the smallest.  */
 35652                   Lisp_Object lim1
 35653                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35654                     ? Fmarker_position (w->start)
 35655                     : Qnil;
 35656                   Lisp_Object lim2
 35657                     = NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
 35658                     ? make_fixnum (BUF_Z (XBUFFER (buffer))
 35659                                    - w->window_end_pos)
 35660                     : Qnil;
 35661 
 35662                   if (NILP (overlay))
 35663                     {
 35664                       /* Handle the text property case.  */
 35665                       before = Fprevious_single_property_change
 35666                         (make_fixnum (pos + 1), Qmouse_face, buffer, lim1);
 35667                       after = Fnext_single_property_change
 35668                         (make_fixnum (pos), Qmouse_face, buffer, lim2);
 35669                       before_string = after_string = Qnil;
 35670                     }
 35671                   else
 35672                     {
 35673                       /* Handle the overlay case.  */
 35674                       before = Foverlay_start (overlay);
 35675                       after = Foverlay_end (overlay);
 35676                       before_string = Foverlay_get (overlay, Qbefore_string);
 35677                       after_string = Foverlay_get (overlay, Qafter_string);
 35678 
 35679                       if (!STRINGP (before_string)) before_string = Qnil;
 35680                       if (!STRINGP (after_string))  after_string = Qnil;
 35681                     }
 35682 
 35683                   mouse_face_from_buffer_pos (window, hlinfo, pos,
 35684                                               NILP (before)
 35685                                               ? 1
 35686                                               : XFIXNAT (before),
 35687                                               NILP (after)
 35688                                               ? BUF_Z (XBUFFER (buffer))
 35689                                               : XFIXNAT (after),
 35690                                               before_string, after_string,
 35691                                               disp_string);
 35692                   cursor = No_Cursor;
 35693                 }
 35694             }
 35695         }
 35696 
 35697     check_help_echo:
 35698 
 35699       /* Look for a `help-echo' property.  */
 35700       if (NILP (help_echo_string)) {
 35701         Lisp_Object help, overlay;
 35702 
 35703         /* Check overlays first.  */
 35704         help = overlay = Qnil;
 35705         for (i = noverlays - 1; i >= 0 && NILP (help); --i)
 35706           {
 35707             overlay = overlay_vec[i];
 35708             help = Foverlay_get (overlay, Qhelp_echo);
 35709           }
 35710 
 35711         if (!NILP (help))
 35712           {
 35713             help_echo_string = help;
 35714             help_echo_window = window;
 35715             help_echo_object = overlay;
 35716             help_echo_pos = pos;
 35717           }
 35718         else
 35719           {
 35720             Lisp_Object obj = glyph->object;
 35721             ptrdiff_t charpos = glyph->charpos;
 35722 
 35723             /* Try text properties.  */
 35724             if (STRINGP (obj)
 35725                 && charpos >= 0
 35726                 && charpos < SCHARS (obj))
 35727               {
 35728                 help = Fget_text_property (make_fixnum (charpos),
 35729                                            Qhelp_echo, obj);
 35730                 if (NILP (help))
 35731                   {
 35732                     /* If the string itself doesn't specify a help-echo,
 35733                        see if the buffer text ``under'' it does.  */
 35734                     struct glyph_row *r
 35735                       = MATRIX_ROW (w->current_matrix, vpos);
 35736                     ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35737                     ptrdiff_t p = string_buffer_position (obj, start);
 35738                     if (p > 0)
 35739                       {
 35740                         help = Fget_char_property (make_fixnum (p),
 35741                                                    Qhelp_echo, w->contents);
 35742                         if (!NILP (help))
 35743                           {
 35744                             charpos = p;
 35745                             obj = w->contents;
 35746                           }
 35747                       }
 35748                   }
 35749               }
 35750             else if (BUFFERP (obj)
 35751                      && charpos >= BEGV
 35752                      && charpos < ZV)
 35753               help = Fget_text_property (make_fixnum (charpos), Qhelp_echo,
 35754                                          obj);
 35755 
 35756             if (!NILP (help))
 35757               {
 35758                 help_echo_string = help;
 35759                 help_echo_window = window;
 35760                 help_echo_object = obj;
 35761                 help_echo_pos = charpos;
 35762               }
 35763           }
 35764       }
 35765 
 35766 #ifdef HAVE_WINDOW_SYSTEM
 35767       /* Look for a `pointer' property.  */
 35768       if (FRAME_WINDOW_P (f) && NILP (pointer))
 35769         {
 35770           /* Check overlays first.  */
 35771           for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
 35772             pointer = Foverlay_get (overlay_vec[i], Qpointer);
 35773 
 35774           if (NILP (pointer))
 35775             {
 35776               Lisp_Object obj = glyph->object;
 35777               ptrdiff_t charpos = glyph->charpos;
 35778 
 35779               /* Try text properties.  */
 35780               if (STRINGP (obj)
 35781                   && charpos >= 0
 35782                   && charpos < SCHARS (obj))
 35783                 {
 35784                   pointer = Fget_text_property (make_fixnum (charpos),
 35785                                                 Qpointer, obj);
 35786                   if (NILP (pointer))
 35787                     {
 35788                       /* If the string itself doesn't specify a pointer,
 35789                          see if the buffer text ``under'' it does.  */
 35790                       struct glyph_row *r
 35791                         = MATRIX_ROW (w->current_matrix, vpos);
 35792                       ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
 35793                       ptrdiff_t p = string_buffer_position (obj, start);
 35794                       if (p > 0)
 35795                         pointer = Fget_char_property (make_fixnum (p),
 35796                                                       Qpointer, w->contents);
 35797                     }
 35798                 }
 35799               else if (BUFFERP (obj)
 35800                        && charpos >= BEGV
 35801                        && charpos < ZV)
 35802                 pointer = Fget_text_property (make_fixnum (charpos),
 35803                                               Qpointer, obj);
 35804             }
 35805         }
 35806 #endif  /* HAVE_WINDOW_SYSTEM */
 35807 
 35808       BEGV = obegv;
 35809       ZV = ozv;
 35810       current_buffer = obuf;
 35811       SAFE_FREE ();
 35812     }
 35813 
 35814  set_cursor:
 35815   define_frame_cursor1 (f, cursor, pointer);
 35816 }
 35817 
 35818 
 35819 /* EXPORT for RIF:
 35820    Clear any mouse-face on window W.  This function is part of the
 35821    redisplay interface, and is called from try_window_id and similar
 35822    functions to ensure the mouse-highlight is off.  */
 35823 
 35824 void
 35825 gui_clear_window_mouse_face (struct window *w)
 35826 {
 35827   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
 35828   Lisp_Object window;
 35829 
 35830   block_input ();
 35831   XSETWINDOW (window, w);
 35832   if (EQ (window, hlinfo->mouse_face_window))
 35833     clear_mouse_face (hlinfo);
 35834   unblock_input ();
 35835 }
 35836 
 35837 
 35838 /* EXPORT:
 35839    Just discard the mouse face information for frame F, if any.
 35840    This is used when the size of F is changed.  */
 35841 
 35842 void
 35843 cancel_mouse_face (struct frame *f)
 35844 {
 35845   Lisp_Object window;
 35846   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 35847 
 35848   window = hlinfo->mouse_face_window;
 35849   if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
 35850     reset_mouse_highlight (hlinfo);
 35851 }
 35852 
 35853 
 35854 
 35855 /***********************************************************************
 35856                            Exposure Events
 35857  ***********************************************************************/
 35858 
 35859 #ifdef HAVE_WINDOW_SYSTEM
 35860 
 35861 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
 35862    which intersects rectangle R.  R is in window-relative coordinates.  */
 35863 
 35864 static void
 35865 expose_area (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r,
 35866              enum glyph_row_area area)
 35867 {
 35868   struct glyph *first = row->glyphs[area];
 35869   struct glyph *end = row->glyphs[area] + row->used[area];
 35870   struct glyph *last;
 35871   int first_x, start_x, x;
 35872 
 35873   if (area == TEXT_AREA && row->fill_line_p)
 35874     /* If row extends face to end of line write the whole line.  */
 35875     draw_glyphs (w, row->x, row, area,
 35876                  0, row->used[area],
 35877                  DRAW_NORMAL_TEXT, 0);
 35878   else
 35879     {
 35880       /* Set START_X to the window-relative start position for drawing glyphs of
 35881          AREA.  The first glyph of the text area can be partially visible.
 35882          The first glyphs of other areas cannot.  */
 35883       start_x = window_box_left_offset (w, area);
 35884       x = start_x;
 35885       if (area == TEXT_AREA)
 35886         x += row->x;
 35887 
 35888       /* Find the first glyph that must be redrawn.  */
 35889       while (first < end
 35890              && x + first->pixel_width < r->x)
 35891         {
 35892           x += first->pixel_width;
 35893           ++first;
 35894         }
 35895 
 35896       /* Find the last one.  */
 35897       last = first;
 35898       first_x = x;
 35899       /* Use a signed int intermediate value to avoid catastrophic
 35900          failures due to comparison between signed and unsigned, when
 35901          x is negative (can happen for wide images that are hscrolled).  */
 35902       int r_end = r->x + r->width;
 35903       while (last < end && x < r_end)
 35904         {
 35905           x += last->pixel_width;
 35906           ++last;
 35907         }
 35908 
 35909       /* Repaint.  */
 35910       if (last > first)
 35911         draw_glyphs (w, first_x - start_x, row, area,
 35912                      first - row->glyphs[area], last - row->glyphs[area],
 35913                      DRAW_NORMAL_TEXT, 0);
 35914     }
 35915 }
 35916 
 35917 
 35918 /* Redraw the parts of the glyph row ROW on window W intersecting
 35919    rectangle R.  R is in window-relative coordinates.  Value is
 35920    true if mouse-face was overwritten.  */
 35921 
 35922 static bool
 35923 expose_line (struct window *w, struct glyph_row *row, const Emacs_Rectangle *r)
 35924 {
 35925   eassert (row->enabled_p);
 35926 
 35927   if (row->mode_line_p || w->pseudo_window_p)
 35928     draw_glyphs (w, 0, row, TEXT_AREA,
 35929                  0, row->used[TEXT_AREA],
 35930                  DRAW_NORMAL_TEXT, 0);
 35931   else
 35932     {
 35933       if (row->used[LEFT_MARGIN_AREA])
 35934         expose_area (w, row, r, LEFT_MARGIN_AREA);
 35935       if (row->used[TEXT_AREA])
 35936         expose_area (w, row, r, TEXT_AREA);
 35937       if (row->used[RIGHT_MARGIN_AREA])
 35938         expose_area (w, row, r, RIGHT_MARGIN_AREA);
 35939       draw_row_fringe_bitmaps (w, row);
 35940     }
 35941 
 35942   return row->mouse_face_p;
 35943 }
 35944 
 35945 
 35946 /* Redraw those parts of glyphs rows during expose event handling that
 35947    overlap other rows.  Redrawing of an exposed line writes over parts
 35948    of lines overlapping that exposed line; this function fixes that.
 35949 
 35950    W is the window being exposed.  FIRST_OVERLAPPING_ROW is the first
 35951    row in W's current matrix that is exposed and overlaps other rows.
 35952    LAST_OVERLAPPING_ROW is the last such row.  */
 35953 
 35954 static void
 35955 expose_overlaps (struct window *w,
 35956                  struct glyph_row *first_overlapping_row,
 35957                  struct glyph_row *last_overlapping_row,
 35958                  const Emacs_Rectangle *r)
 35959 {
 35960   struct glyph_row *row;
 35961 
 35962   for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
 35963     if (row->overlapping_p)
 35964       {
 35965         eassert (row->enabled_p && !row->mode_line_p);
 35966 
 35967         row->clip = r;
 35968         if (row->used[LEFT_MARGIN_AREA])
 35969           gui_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
 35970 
 35971         if (row->used[TEXT_AREA])
 35972           gui_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
 35973 
 35974         if (row->used[RIGHT_MARGIN_AREA])
 35975           gui_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
 35976         row->clip = NULL;
 35977       }
 35978 }
 35979 
 35980 
 35981 /* Return true if W's cursor intersects rectangle R.  */
 35982 
 35983 static bool
 35984 phys_cursor_in_rect_p (struct window *w, const Emacs_Rectangle *r)
 35985 {
 35986   Emacs_Rectangle cr, result;
 35987   struct glyph *cursor_glyph;
 35988   struct glyph_row *row;
 35989 
 35990   if (w->phys_cursor.vpos >= 0
 35991       && w->phys_cursor.vpos < w->current_matrix->nrows
 35992       && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
 35993           row->enabled_p)
 35994       && row->cursor_in_fringe_p)
 35995     {
 35996       /* Cursor is in the fringe.  */
 35997       cr.x = window_box_right_offset (w,
 35998                                       (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
 35999                                        ? RIGHT_MARGIN_AREA
 36000                                        : TEXT_AREA));
 36001       cr.y = row->y;
 36002       cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
 36003       cr.height = row->height;
 36004       return gui_intersect_rectangles (&cr, r, &result);
 36005     }
 36006 
 36007   cursor_glyph = get_phys_cursor_glyph (w);
 36008   if (cursor_glyph)
 36009     {
 36010       /* r is relative to W's box, but w->phys_cursor.x is relative
 36011          to left edge of W's TEXT area.  Adjust it.  */
 36012       cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
 36013       cr.y = w->phys_cursor.y;
 36014       cr.width = cursor_glyph->pixel_width;
 36015       cr.height = w->phys_cursor_height;
 36016       /* ++KFS: W32 version used W32-specific IntersectRect here, but
 36017          I assume the effect is the same -- and this is portable.  */
 36018       return gui_intersect_rectangles (&cr, r, &result);
 36019     }
 36020   /* If we don't understand the format, pretend we're not in the hot-spot.  */
 36021   return false;
 36022 }
 36023 
 36024 
 36025 /* EXPORT:
 36026    Draw a vertical window border to the right of window W if W doesn't
 36027    have vertical scroll bars.  */
 36028 
 36029 void
 36030 gui_draw_vertical_border (struct window *w)
 36031 {
 36032   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36033 
 36034   /* We could do better, if we knew what type of scroll-bar the adjacent
 36035      windows (on either side) have...  But we don't :-(
 36036      However, I think this works ok.  ++KFS 2003-04-25 */
 36037 
 36038   /* Redraw borders between horizontally adjacent windows.  Don't
 36039      do it for frames with vertical scroll bars because either the
 36040      right scroll bar of a window, or the left scroll bar of its
 36041      neighbor will suffice as a border.  */
 36042   if (FRAME_HAS_VERTICAL_SCROLL_BARS (f) || FRAME_RIGHT_DIVIDER_WIDTH (f))
 36043     return;
 36044 
 36045   /* Note: It is necessary to redraw both the left and the right
 36046      borders, for when only this single window W is being
 36047      redisplayed.  */
 36048   if (!WINDOW_RIGHTMOST_P (w)
 36049       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
 36050     {
 36051       int x0, x1, y0, y1;
 36052 
 36053       window_box_edges (w, &x0, &y0, &x1, &y1);
 36054       y1 -= 1;
 36055 
 36056       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36057         x1 -= 1;
 36058 
 36059       FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
 36060     }
 36061 
 36062   if (!WINDOW_LEFTMOST_P (w)
 36063       && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
 36064     {
 36065       int x0, x1, y0, y1;
 36066 
 36067       window_box_edges (w, &x0, &y0, &x1, &y1);
 36068       y1 -= 1;
 36069 
 36070       if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
 36071         x0 -= 1;
 36072 
 36073       FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
 36074     }
 36075 }
 36076 
 36077 
 36078 /* Draw window dividers for window W.  */
 36079 
 36080 void
 36081 gui_draw_right_divider (struct window *w)
 36082 {
 36083   struct frame *f = WINDOW_XFRAME (w);
 36084 
 36085   if (w->mini || w->pseudo_window_p)
 36086     return;
 36087   else if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36088     {
 36089       int x0 = WINDOW_RIGHT_EDGE_X (w) - WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36090       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36091       int y0 = WINDOW_TOP_EDGE_Y (w);
 36092       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36093 
 36094       /* If W is horizontally combined and has a right sibling, don't
 36095          draw over any bottom divider.  */
 36096       if (WINDOW_BOTTOM_DIVIDER_WIDTH (w)
 36097           && !NILP (w->parent)
 36098           && WINDOW_HORIZONTAL_COMBINATION_P (XWINDOW (w->parent))
 36099           && !NILP (w->next))
 36100         y1 -= WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36101 
 36102       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36103     }
 36104 }
 36105 
 36106 static void
 36107 gui_draw_bottom_divider (struct window *w)
 36108 {
 36109   struct frame *f = XFRAME (WINDOW_FRAME (w));
 36110 
 36111   if (w->mini || w->pseudo_window_p)
 36112     return;
 36113   else if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36114     {
 36115       int x0 = WINDOW_LEFT_EDGE_X (w);
 36116       int x1 = WINDOW_RIGHT_EDGE_X (w);
 36117       int y0 = WINDOW_BOTTOM_EDGE_Y (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
 36118       int y1 = WINDOW_BOTTOM_EDGE_Y (w);
 36119       struct window *p = !NILP (w->parent) ? XWINDOW (w->parent) : NULL;
 36120 
 36121       /* If W is vertically combined and has a sibling below, don't draw
 36122          over any right divider.  */
 36123       if (WINDOW_RIGHT_DIVIDER_WIDTH (w)
 36124           && p
 36125           && ((WINDOW_VERTICAL_COMBINATION_P (p)
 36126                && !NILP (w->next))
 36127               || (WINDOW_HORIZONTAL_COMBINATION_P (p)
 36128                   && NILP (w->next)
 36129                   && !NILP (p->parent)
 36130                   && WINDOW_VERTICAL_COMBINATION_P (XWINDOW (p->parent))
 36131                   && !NILP (XWINDOW (p->parent)->next))))
 36132         x1 -= WINDOW_RIGHT_DIVIDER_WIDTH (w);
 36133 
 36134       FRAME_RIF (f)->draw_window_divider (w, x0, x1, y0, y1);
 36135     }
 36136 }
 36137 
 36138 /* Redraw the part of window W intersection rectangle FR.  Pixel
 36139    coordinates in FR are frame-relative.  Call this function with
 36140    input blocked.  Value is true if the exposure overwrites
 36141    mouse-face.  */
 36142 
 36143 static bool
 36144 expose_window (struct window *w, const Emacs_Rectangle *fr)
 36145 {
 36146   struct frame *f = XFRAME (w->frame);
 36147   Emacs_Rectangle wr, r;
 36148   bool mouse_face_overwritten_p = false;
 36149 
 36150   /* If window is not yet fully initialized, do nothing.  This can
 36151      happen when toolkit scroll bars are used and a window is split.
 36152      Reconfiguring the scroll bar will generate an expose for a newly
 36153      created window.  */
 36154   if (w->current_matrix == NULL)
 36155     return false;
 36156 
 36157   /* When we're currently updating the window, display and current
 36158      matrix usually don't agree.  Arrange for a thorough display
 36159      later.  */
 36160   if (w->must_be_updated_p)
 36161     {
 36162       SET_FRAME_GARBAGED (f);
 36163       return false;
 36164     }
 36165 
 36166   /* Frame-relative pixel rectangle of W.  */
 36167   wr.x = WINDOW_LEFT_EDGE_X (w);
 36168   wr.y = WINDOW_TOP_EDGE_Y (w);
 36169   wr.width = WINDOW_PIXEL_WIDTH (w);
 36170   wr.height = WINDOW_PIXEL_HEIGHT (w);
 36171 
 36172   if (gui_intersect_rectangles (fr, &wr, &r))
 36173     {
 36174       int yb = window_text_bottom_y (w);
 36175       struct glyph_row *row;
 36176       struct glyph_row *first_overlapping_row, *last_overlapping_row;
 36177 
 36178       redisplay_trace ("expose_window (%d, %d, %u, %u)\n",
 36179                        r.x, r.y, r.width, r.height);
 36180 
 36181       /* Convert to window coordinates.  */
 36182       r.x -= WINDOW_LEFT_EDGE_X (w);
 36183       r.y -= WINDOW_TOP_EDGE_Y (w);
 36184 
 36185       /* Turn off the cursor.  */
 36186       bool cursor_cleared_p = (!w->pseudo_window_p
 36187                                && phys_cursor_in_rect_p (w, &r));
 36188       if (cursor_cleared_p)
 36189         gui_clear_cursor (w);
 36190 
 36191       /* If the row containing the cursor extends face to end of line,
 36192          then expose_area might overwrite the cursor outside the
 36193          rectangle and thus notice_overwritten_cursor might clear
 36194          w->phys_cursor_on_p.  We remember the original value and
 36195          check later if it is changed.  */
 36196       bool phys_cursor_on_p = w->phys_cursor_on_p;
 36197 
 36198       /* Use a signed int intermediate value to avoid catastrophic
 36199          failures due to comparison between signed and unsigned, when
 36200          y0 or y1 is negative (can happen for tall images).  */
 36201       int r_bottom = r.y + r.height;
 36202 
 36203       /* We must temporarily switch to the window's buffer, in case
 36204          the fringe face has been remapped in that buffer's
 36205          face-remapping-alist, so that draw_row_fringe_bitmaps,
 36206          called from expose_line, will use the right face.  */
 36207       bool buffer_changed = false;
 36208       struct buffer *oldbuf = current_buffer;
 36209       if (!w->pseudo_window_p)
 36210         {
 36211           set_buffer_internal_1 (XBUFFER (w->contents));
 36212           buffer_changed = true;
 36213         }
 36214 
 36215       /* Update lines intersecting rectangle R.  */
 36216       first_overlapping_row = last_overlapping_row = NULL;
 36217       for (row = w->current_matrix->rows;
 36218            row->enabled_p;
 36219            ++row)
 36220         {
 36221           int y0 = row->y;
 36222           int y1 = MATRIX_ROW_BOTTOM_Y (row);
 36223 
 36224           if ((y0 >= r.y && y0 < r_bottom)
 36225               || (y1 > r.y && y1 < r_bottom)
 36226               || (r.y >= y0 && r.y < y1)
 36227               || (r_bottom > y0 && r_bottom < y1))
 36228             {
 36229               /* A header line may be overlapping, but there is no need
 36230                  to fix overlapping areas for them.  KFS 2005-02-12 */
 36231               if (row->overlapping_p && !row->mode_line_p)
 36232                 {
 36233                   if (first_overlapping_row == NULL)
 36234                     first_overlapping_row = row;
 36235                   last_overlapping_row = row;
 36236                 }
 36237 
 36238               row->clip = fr;
 36239               if (expose_line (w, row, &r))
 36240                 mouse_face_overwritten_p = true;
 36241               row->clip = NULL;
 36242             }
 36243           else if (row->overlapping_p)
 36244             {
 36245               /* We must redraw a row overlapping the exposed area.  */
 36246               if (y0 < r.y
 36247                   ? y0 + row->phys_height > r.y
 36248                   : y0 + row->ascent - row->phys_ascent < r.y +r.height)
 36249                 {
 36250                   if (first_overlapping_row == NULL)
 36251                     first_overlapping_row = row;
 36252                   last_overlapping_row = row;
 36253                 }
 36254             }
 36255 
 36256           if (y1 >= yb)
 36257             break;
 36258         }
 36259 
 36260       if (buffer_changed)
 36261         set_buffer_internal_1 (oldbuf);
 36262 
 36263       /* Display the mode line if there is one.  */
 36264       if (window_wants_mode_line (w)
 36265           && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
 36266               row->enabled_p)
 36267           && row->y < r_bottom)
 36268         {
 36269           if (expose_line (w, row, &r))
 36270             mouse_face_overwritten_p = true;
 36271         }
 36272 
 36273       if (!w->pseudo_window_p)
 36274         {
 36275           /* Fix the display of overlapping rows.  */
 36276           if (first_overlapping_row)
 36277             expose_overlaps (w, first_overlapping_row, last_overlapping_row,
 36278                              fr);
 36279 
 36280           /* Draw border between windows.  */
 36281           if (WINDOW_RIGHT_DIVIDER_WIDTH (w))
 36282             gui_draw_right_divider (w);
 36283           else
 36284             gui_draw_vertical_border (w);
 36285 
 36286           if (WINDOW_BOTTOM_DIVIDER_WIDTH (w))
 36287             gui_draw_bottom_divider (w);
 36288 
 36289           /* Turn the cursor on again.  */
 36290           if (cursor_cleared_p
 36291               || (phys_cursor_on_p && !w->phys_cursor_on_p))
 36292             update_window_cursor (w, true);
 36293         }
 36294     }
 36295 
 36296   return mouse_face_overwritten_p;
 36297 }
 36298 
 36299 
 36300 
 36301 /* Redraw (parts) of all windows in the window tree rooted at W that
 36302    intersect R.  R contains frame pixel coordinates.  Value is
 36303    true if the exposure overwrites mouse-face.  */
 36304 
 36305 static bool
 36306 expose_window_tree (struct window *w, const Emacs_Rectangle *r)
 36307 {
 36308   struct frame *f = XFRAME (w->frame);
 36309   bool mouse_face_overwritten_p = false;
 36310 
 36311   while (w && !FRAME_GARBAGED_P (f))
 36312     {
 36313       mouse_face_overwritten_p
 36314         |= (WINDOWP (w->contents)
 36315             ? expose_window_tree (XWINDOW (w->contents), r)
 36316             : expose_window (w, r));
 36317 
 36318       w = NILP (w->next) ? NULL : XWINDOW (w->next);
 36319     }
 36320 
 36321   return mouse_face_overwritten_p;
 36322 }
 36323 
 36324 
 36325 /* EXPORT:
 36326    Redisplay an exposed area of frame F.  X and Y are the upper-left
 36327    corner of the exposed rectangle.  W and H are width and height of
 36328    the exposed area.  All are pixel values.  W or H zero means redraw
 36329    the entire frame.  */
 36330 
 36331 void
 36332 expose_frame (struct frame *f, int x, int y, int w, int h)
 36333 {
 36334   Emacs_Rectangle r;
 36335   bool mouse_face_overwritten_p = false;
 36336 
 36337   if (FRAME_GARBAGED_P (f))
 36338     {
 36339       redisplay_trace ("expose_frame garbaged\n");
 36340       return;
 36341     }
 36342 
 36343   /* If basic faces haven't been realized yet, there is no point in
 36344      trying to redraw anything.  This can happen when we get an expose
 36345      event while Emacs is starting, e.g. by moving another window.  */
 36346   if (FRAME_FACE_CACHE (f) == NULL
 36347       || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
 36348     {
 36349       redisplay_trace ("expose_frame no faces\n");
 36350       return;
 36351     }
 36352 
 36353   if (w == 0 || h == 0)
 36354     {
 36355       r.x = r.y = 0;
 36356       r.width = FRAME_TEXT_WIDTH (f);
 36357       r.height = FRAME_TEXT_HEIGHT (f);
 36358     }
 36359   else
 36360     {
 36361       r.x = x;
 36362       r.y = y;
 36363       r.width = w;
 36364       r.height = h;
 36365     }
 36366 
 36367   redisplay_trace ("expose_frame (%d, %d, %u, %u)\n",
 36368                    r.x, r.y, r.width, r.height);
 36369   mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
 36370 
 36371   if (WINDOWP (f->tab_bar_window))
 36372     mouse_face_overwritten_p
 36373       |= expose_window (XWINDOW (f->tab_bar_window), &r);
 36374 
 36375 #ifndef HAVE_EXT_TOOL_BAR
 36376   if (WINDOWP (f->tool_bar_window))
 36377     mouse_face_overwritten_p
 36378       |= expose_window (XWINDOW (f->tool_bar_window), &r);
 36379 #endif
 36380 
 36381 #ifdef HAVE_X_WINDOWS
 36382 #ifndef MSDOS
 36383 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
 36384   if (WINDOWP (f->menu_bar_window))
 36385     mouse_face_overwritten_p
 36386       |= expose_window (XWINDOW (f->menu_bar_window), &r);
 36387 #endif /* not USE_X_TOOLKIT and not USE_GTK */
 36388 #endif
 36389 #endif
 36390 
 36391   /* Some window managers support a focus-follows-mouse style with
 36392      delayed raising of frames.  Imagine a partially obscured frame,
 36393      and moving the mouse into partially obscured mouse-face on that
 36394      frame.  The visible part of the mouse-face will be highlighted,
 36395      then the WM raises the obscured frame.  With at least one WM, KDE
 36396      2.1, Emacs is not getting any event for the raising of the frame
 36397      (even tried with SubstructureRedirectMask), only Expose events.
 36398      These expose events will draw text normally, i.e. not
 36399      highlighted.  Which means we must redo the highlight here.
 36400      Subsume it under ``we love X''.  --gerd 2001-08-15  */
 36401   /* Included in Windows version because Windows most likely does not
 36402      do the right thing if any third party tool offers
 36403      focus-follows-mouse with delayed raise.  --jason 2001-10-12  */
 36404   if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
 36405     {
 36406       Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 36407       if (f == hlinfo->mouse_face_mouse_frame)
 36408         {
 36409           int mouse_x = hlinfo->mouse_face_mouse_x;
 36410           int mouse_y = hlinfo->mouse_face_mouse_y;
 36411           clear_mouse_face (hlinfo);
 36412           note_mouse_highlight (f, mouse_x, mouse_y);
 36413         }
 36414     }
 36415 }
 36416 
 36417 
 36418 /* EXPORT:
 36419    Determine the intersection of two rectangles R1 and R2.  Return
 36420    the intersection in *RESULT.  Value is true if RESULT is not
 36421    empty.  */
 36422 
 36423 bool
 36424 gui_intersect_rectangles (const Emacs_Rectangle *r1, const Emacs_Rectangle *r2,
 36425                           Emacs_Rectangle *result)
 36426 {
 36427   const Emacs_Rectangle *left, *right;
 36428   const Emacs_Rectangle *upper, *lower;
 36429   bool intersection_p = false;
 36430 
 36431   /* Rearrange so that R1 is the left-most rectangle.  */
 36432   if (r1->x < r2->x)
 36433     left = r1, right = r2;
 36434   else
 36435     left = r2, right = r1;
 36436 
 36437   /* X0 of the intersection is right.x0, if this is inside R1,
 36438      otherwise there is no intersection.  */
 36439   if (right->x <= left->x + left->width)
 36440     {
 36441       result->x = right->x;
 36442 
 36443       /* The right end of the intersection is the minimum of
 36444          the right ends of left and right.  */
 36445       result->width = (min (left->x + left->width, right->x + right->width)
 36446                        - result->x);
 36447 
 36448       /* Same game for Y.  */
 36449       if (r1->y < r2->y)
 36450         upper = r1, lower = r2;
 36451       else
 36452         upper = r2, lower = r1;
 36453 
 36454       /* The upper end of the intersection is lower.y0, if this is inside
 36455          of upper.  Otherwise, there is no intersection.  */
 36456       if (lower->y <= upper->y + upper->height)
 36457         {
 36458           result->y = lower->y;
 36459 
 36460           /* The lower end of the intersection is the minimum of the lower
 36461              ends of upper and lower.  */
 36462           result->height = (min (lower->y + lower->height,
 36463                                  upper->y + upper->height)
 36464                             - result->y);
 36465           intersection_p = true;
 36466         }
 36467     }
 36468 
 36469   return intersection_p;
 36470 }
 36471 
 36472 #endif /* HAVE_WINDOW_SYSTEM */
 36473 
 36474 
 36475 /***********************************************************************
 36476                             Initialization
 36477  ***********************************************************************/
 36478 
 36479 void
 36480 syms_of_xdisp (void)
 36481 {
 36482   Vwith_echo_area_save_vector = Qnil;
 36483   staticpro (&Vwith_echo_area_save_vector);
 36484 
 36485   Vmessage_stack = Qnil;
 36486   staticpro (&Vmessage_stack);
 36487 
 36488   /* Non-nil means don't actually do any redisplay.  */
 36489   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
 36490 
 36491   DEFSYM (Qredisplay_internal_xC_functionx, "redisplay_internal (C function)");
 36492 
 36493   DEFVAR_BOOL ("scroll-minibuffer-conservatively",
 36494                scroll_minibuffer_conservatively,
 36495                doc: /* Non-nil means scroll conservatively in minibuffer windows.
 36496 When the value is nil, scrolling in minibuffer windows obeys the
 36497 settings of `scroll-conservatively'.  */);
 36498   scroll_minibuffer_conservatively = true; /* bug#44070 */
 36499 
 36500   DEFVAR_BOOL ("inhibit-message", inhibit_message,
 36501               doc:  /* Non-nil means calls to `message' are not displayed.
 36502 They are still logged to the *Messages* buffer.
 36503 
 36504 Do NOT set this globally to a non-nil value, as doing that will
 36505 disable messages everywhere, including in I-search and other
 36506 places where they are necessary.  This variable is intended to
 36507 be let-bound around code that needs to disable messages temporarily. */);
 36508   inhibit_message = false;
 36509 
 36510   message_dolog_marker1 = Fmake_marker ();
 36511   staticpro (&message_dolog_marker1);
 36512   message_dolog_marker2 = Fmake_marker ();
 36513   staticpro (&message_dolog_marker2);
 36514   message_dolog_marker3 = Fmake_marker ();
 36515   staticpro (&message_dolog_marker3);
 36516 
 36517   defsubr (&Sset_buffer_redisplay);
 36518 #ifdef GLYPH_DEBUG
 36519   defsubr (&Sdump_frame_glyph_matrix);
 36520   defsubr (&Sdump_glyph_matrix);
 36521   defsubr (&Sdump_glyph_row);
 36522   defsubr (&Sdump_tab_bar_row);
 36523   defsubr (&Sdump_tool_bar_row);
 36524   defsubr (&Strace_redisplay);
 36525   defsubr (&Strace_to_stderr);
 36526 #endif
 36527 #ifdef HAVE_WINDOW_SYSTEM
 36528   defsubr (&Stab_bar_height);
 36529   defsubr (&Stool_bar_height);
 36530   defsubr (&Slookup_image_map);
 36531 #endif
 36532   defsubr (&Sline_pixel_height);
 36533   defsubr (&Sformat_mode_line);
 36534   defsubr (&Sinvisible_p);
 36535   defsubr (&Scurrent_bidi_paragraph_direction);
 36536   defsubr (&Swindow_text_pixel_size);
 36537   defsubr (&Sbuffer_text_pixel_size);
 36538   defsubr (&Smove_point_visually);
 36539   defsubr (&Sbidi_find_overridden_directionality);
 36540   defsubr (&Sdisplay__line_is_continued_p);
 36541   defsubr (&Sget_display_property);
 36542   defsubr (&Slong_line_optimizations_p);
 36543 
 36544   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
 36545   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
 36546   DEFSYM (Qoverriding_local_map, "overriding-local-map");
 36547   DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
 36548   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
 36549   DEFSYM (Qeval, "eval");
 36550   DEFSYM (QCdata, ":data");
 36551 
 36552   /* Names of text properties relevant for redisplay.  */
 36553   DEFSYM (Qdisplay, "display");
 36554   DEFSYM (Qspace_width, "space-width");
 36555   DEFSYM (Qraise, "raise");
 36556   DEFSYM (Qslice, "slice");
 36557   DEFSYM (Qspace, "space");
 36558   DEFSYM (Qmargin, "margin");
 36559   DEFSYM (Qpointer, "pointer");
 36560   DEFSYM (Qleft_margin, "left-margin");
 36561   DEFSYM (Qright_margin, "right-margin");
 36562   DEFSYM (Qcenter, "center");
 36563   DEFSYM (Qline_height, "line-height");
 36564   DEFSYM (QCalign_to, ":align-to");
 36565   DEFSYM (QCrelative_width, ":relative-width");
 36566   DEFSYM (QCrelative_height, ":relative-height");
 36567   DEFSYM (QCeval, ":eval");
 36568   DEFSYM (QCpropertize, ":propertize");
 36569   DEFSYM (QCfile, ":file");
 36570   DEFSYM (Qfontified, "fontified");
 36571   DEFSYM (Qfontification_functions, "fontification-functions");
 36572   DEFSYM (Qlong_line_optimizations_in_fontification_functions,
 36573           "long-line-optimizations-in-fontification-functions");
 36574 
 36575   /* Name of the symbol which disables Lisp evaluation in 'display'
 36576      properties.  This is used by enriched.el.  */
 36577   DEFSYM (Qdisable_eval, "disable-eval");
 36578 
 36579   /* Name of the face used to highlight trailing whitespace.  */
 36580   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
 36581 
 36582   /* Names of the faces used to display line numbers.  */
 36583   DEFSYM (Qline_number, "line-number");
 36584   DEFSYM (Qline_number_current_line, "line-number-current-line");
 36585   DEFSYM (Qline_number_major_tick, "line-number-major-tick");
 36586   DEFSYM (Qline_number_minor_tick, "line-number-minor-tick");
 36587   /* Name of a text property which disables line-number display.  */
 36588   DEFSYM (Qdisplay_line_numbers_disable, "display-line-numbers-disable");
 36589 
 36590   /* Name of the face used to display fill column indicator character.  */
 36591   DEFSYM (Qfill_column_indicator, "fill-column-indicator");
 36592 
 36593   /* Name and number of the face used to highlight escape glyphs.  */
 36594   DEFSYM (Qescape_glyph, "escape-glyph");
 36595 
 36596   /* Name and number of the face used to highlight non-breaking
 36597      spaces/hyphens.  */
 36598   DEFSYM (Qnobreak_space, "nobreak-space");
 36599   DEFSYM (Qnobreak_hyphen, "nobreak-hyphen");
 36600 
 36601   /* The symbol 'image' which is the car of the lists used to represent
 36602      images in Lisp.  Also a tool bar style.  */
 36603   DEFSYM (Qimage, "image");
 36604 
 36605   /* Tool bar styles.  */
 36606   DEFSYM (Qtext, "text");
 36607   DEFSYM (Qboth, "both");
 36608   DEFSYM (Qboth_horiz, "both-horiz");
 36609   DEFSYM (Qtext_image_horiz, "text-image-horiz");
 36610 
 36611   /* The image map types.  */
 36612   DEFSYM (QCmap, ":map");
 36613   DEFSYM (QCpointer, ":pointer");
 36614   DEFSYM (Qrect, "rect");
 36615   DEFSYM (Qcircle, "circle");
 36616   DEFSYM (Qpoly, "poly");
 36617 
 36618   DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
 36619 
 36620   DEFSYM (Qgrow_only, "grow-only");
 36621   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
 36622   DEFSYM (Qposition, "position");
 36623   DEFSYM (Qbuffer_position, "buffer-position");
 36624   DEFSYM (Qobject, "object");
 36625 
 36626   /* Cursor shapes.  */
 36627   DEFSYM (Qbar, "bar");
 36628   DEFSYM (Qhbar, "hbar");
 36629   DEFSYM (Qbox, "box");
 36630   DEFSYM (Qhollow, "hollow");
 36631 
 36632   /* Pointer shapes.  */
 36633   DEFSYM (Qhand, "hand");
 36634   DEFSYM (Qarrow, "arrow");
 36635   /* also Qtext */
 36636 
 36637   DEFSYM (Qdragging, "dragging");
 36638   DEFSYM (Qdropping, "dropping");
 36639   DEFSYM (Qdrag_source, "drag-source");
 36640 
 36641   DEFSYM (Qdrag_with_mode_line, "drag-with-mode-line");
 36642   DEFSYM (Qdrag_with_header_line, "drag-with-header-line");
 36643   DEFSYM (Qdrag_with_tab_line, "drag-with-tab-line");
 36644 
 36645   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 36646 
 36647   list_of_error = list1 (Qerror);
 36648   staticpro (&list_of_error);
 36649 
 36650   /* Values of those variables at last redisplay are stored as
 36651      properties on 'overlay-arrow-position' symbol.  However, if
 36652      Voverlay_arrow_position is a marker, last-arrow-position is its
 36653      numerical position.  */
 36654   DEFSYM (Qlast_arrow_position, "last-arrow-position");
 36655   DEFSYM (Qlast_arrow_string, "last-arrow-string");
 36656 
 36657   /* Alternative overlay-arrow-string and overlay-arrow-bitmap
 36658      properties on a symbol in overlay-arrow-variable-list.  */
 36659   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
 36660   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 36661 
 36662   echo_buffer[0] = echo_buffer[1] = Qnil;
 36663   staticpro (&echo_buffer[0]);
 36664   staticpro (&echo_buffer[1]);
 36665 
 36666   echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
 36667   staticpro (&echo_area_buffer[0]);
 36668   staticpro (&echo_area_buffer[1]);
 36669 
 36670   DEFVAR_LISP ("messages-buffer-name", Vmessages_buffer_name,
 36671     doc: /* The name of the buffer where messages are logged.
 36672 This is normally \"\*Messages*\", but can be rebound by packages that
 36673 wish to redirect messages to a different buffer.  (If the buffer
 36674 doesn't exist, it will be created and put into
 36675 `messages-buffer-mode'.)  */);
 36676   Vmessages_buffer_name = build_string ("*Messages*");
 36677 
 36678   mode_line_proptrans_alist = Qnil;
 36679   staticpro (&mode_line_proptrans_alist);
 36680   mode_line_string_list = Qnil;
 36681   staticpro (&mode_line_string_list);
 36682   mode_line_string_face = Qnil;
 36683   staticpro (&mode_line_string_face);
 36684   mode_line_string_face_prop = Qnil;
 36685   staticpro (&mode_line_string_face_prop);
 36686   Vmode_line_unwind_vector = Qnil;
 36687   staticpro (&Vmode_line_unwind_vector);
 36688 
 36689   DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
 36690 
 36691   help_echo_string = Qnil;
 36692   staticpro (&help_echo_string);
 36693   help_echo_object = Qnil;
 36694   staticpro (&help_echo_object);
 36695   help_echo_window = Qnil;
 36696   staticpro (&help_echo_window);
 36697   previous_help_echo_string = Qnil;
 36698   staticpro (&previous_help_echo_string);
 36699   help_echo_pos = -1;
 36700 
 36701   DEFSYM (Qright_to_left, "right-to-left");
 36702   DEFSYM (Qleft_to_right, "left-to-right");
 36703   defsubr (&Sbidi_resolved_levels);
 36704 
 36705 #ifdef HAVE_WINDOW_SYSTEM
 36706   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
 36707     doc: /* Non-nil means draw block cursor as wide as the glyph under it.
 36708 For example, if a block cursor is over a tab, it will be drawn as
 36709 wide as that tab on the display.  */);
 36710   x_stretch_cursor_p = 0;
 36711 #endif
 36712 
 36713   DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
 36714     doc: /* Non-nil means highlight trailing whitespace.
 36715 The face used for trailing whitespace is `trailing-whitespace'.  */);
 36716   Vshow_trailing_whitespace = Qnil;
 36717 
 36718   DEFVAR_LISP ("mode-line-compact", Vmode_line_compact,
 36719     doc: /* Non-nil means that mode lines should be compact.
 36720 This means that repeating spaces will be replaced with a single space.
 36721 If this variable is `long', only mode lines that are wider than the
 36722 currently selected window are compressed. */);
 36723   Vmode_line_compact = Qnil;
 36724   DEFSYM (Qlong, "long");
 36725 
 36726   DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
 36727     doc: /* Control highlighting of non-ASCII space and hyphen chars.
 36728 If the value is t, Emacs highlights non-ASCII chars which have the
 36729 same appearance as an ASCII space or hyphen, using the `nobreak-space'
 36730 or `nobreak-hyphen' face respectively.
 36731 
 36732 All of the non-ASCII characters in the Unicode horizontal whitespace
 36733 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36734 U+2011 (non-breaking hyphen) are affected.
 36735 
 36736 Any other non-nil value means to display these characters as an escape
 36737 glyph followed by an ordinary space or hyphen.
 36738 
 36739 A value of nil means no special handling of these characters.  */);
 36740   Vnobreak_char_display = Qt;
 36741 
 36742   DEFVAR_BOOL ("nobreak-char-ascii-display", nobreak_char_ascii_display,
 36743     doc: /* Control display of non-ASCII space and hyphen chars.
 36744 If the value of this variable is nil, the default, Emacs displays
 36745 non-ASCII chars which have the same appearance as an ASCII space
 36746 or hyphen as themselves, with the `nobreak-space' or `nobreak-hyphen'
 36747 face, respectively.
 36748 
 36749 If the value is t, these characters are displayed as their ASCII
 36750 counterparts: whitespace characters as ASCII space, hyphen characters
 36751 as ASCII hyphen (a.k.a. \"dash\"), using the `nobreak-space' or
 36752 the `nobreak-hyphen' face.
 36753 
 36754 This variable has effect only if `nobreak-char-display' is t;
 36755 otherwise it is ignored.
 36756 
 36757 All of the non-ASCII characters in the Unicode horizontal whitespace
 36758 character class, as well as U+00AD (soft hyphen), U+2010 (hyphen), and
 36759 U+2011 (non-breaking hyphen) are affected.  */);
 36760   nobreak_char_ascii_display = false;
 36761 
 36762   DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
 36763     doc: /* The pointer shape to show in void text areas.
 36764 A value of nil means to show the text pointer.  Other options are
 36765 `arrow', `text', `hand', `vdrag', `hdrag', `nhdrag', `modeline', and
 36766 `hourglass'.  */);
 36767   Vvoid_text_area_pointer = Qarrow;
 36768 
 36769   DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
 36770     doc: /* Non-nil means don't actually do any redisplay.
 36771 This is used for internal purposes.  */);
 36772   Vinhibit_redisplay = Qnil;
 36773 
 36774   DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
 36775     doc: /* String (or mode line construct) included (normally) in `mode-line-misc-info'.  */);
 36776   Vglobal_mode_string = Qnil;
 36777 
 36778   DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
 36779     doc: /* Marker for where to display an arrow on top of the buffer text.
 36780 This must be the beginning of a line in order to work.
 36781 See also `overlay-arrow-string'.  */);
 36782   Voverlay_arrow_position = Qnil;
 36783 
 36784   DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
 36785     doc: /* String to display as an arrow in non-window frames.
 36786 See also `overlay-arrow-position'.  */);
 36787   Voverlay_arrow_string = build_pure_c_string ("=>");
 36788 
 36789   DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
 36790     doc: /* List of variables (symbols) which hold markers for overlay arrows.
 36791 The symbols on this list are examined during redisplay to determine
 36792 where to display overlay arrows.  */);
 36793   Voverlay_arrow_variable_list
 36794     = list1 (intern_c_string ("overlay-arrow-position"));
 36795 
 36796   DEFVAR_INT ("scroll-step", emacs_scroll_step,
 36797     doc: /* The number of lines to try scrolling a window by when point moves out.
 36798 If that fails to bring point back on frame, point is centered instead.
 36799 If this is zero, point is always centered after it moves off frame.
 36800 If you want scrolling to always be a line at a time, you should set
 36801 `scroll-conservatively' to a large value rather than set this to 1.  */);
 36802 
 36803   DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
 36804     doc: /* Scroll up to this many lines, to bring point back on screen.
 36805 If point moves off-screen, redisplay will scroll by up to
 36806 `scroll-conservatively' lines in order to bring point just barely
 36807 onto the screen again.  If that cannot be done, then redisplay
 36808 recenters point as usual.
 36809 
 36810 If the value is greater than 100, redisplay will never recenter point,
 36811 but will always scroll just enough text to bring point into view, even
 36812 if you move far away.
 36813 
 36814 A value of zero means always recenter point if it moves off screen.  */);
 36815   scroll_conservatively = 0;
 36816 
 36817   DEFVAR_INT ("scroll-margin", scroll_margin,
 36818     doc: /* Number of lines of margin at the top and bottom of a window.
 36819 Trigger automatic scrolling whenever point gets within this many lines
 36820 of the top or bottom of the window (see info node `Auto Scrolling').  */);
 36821   scroll_margin = 0;
 36822 
 36823   DEFVAR_LISP ("maximum-scroll-margin", Vmaximum_scroll_margin,
 36824     doc: /* Maximum effective value of `scroll-margin'.
 36825 Given as a fraction of the current window's lines.  The value should
 36826 be a floating point number between 0.0 and 0.5.  The effective maximum
 36827 is limited to (/ (1- window-lines) 2).  Non-float values for this
 36828 variable are ignored and the default 0.25 is used instead.  */);
 36829   Vmaximum_scroll_margin = make_float (0.25);
 36830 
 36831   DEFVAR_LISP ("display-pixels-per-inch",  Vdisplay_pixels_per_inch,
 36832     doc: /* Pixels per inch value for non-window system displays.
 36833 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI).  */);
 36834   Vdisplay_pixels_per_inch = make_float (72.0);
 36835 
 36836 #ifdef GLYPH_DEBUG
 36837   DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask.  */);
 36838 #endif
 36839 
 36840   DEFVAR_LISP ("truncate-partial-width-windows",
 36841                Vtruncate_partial_width_windows,
 36842     doc: /* Non-nil means truncate lines in windows narrower than the frame.
 36843 For an integer value, truncate lines in each window narrower than the
 36844 full frame width, provided the total window width in column units is less
 36845 than that integer; otherwise, respect the value of `truncate-lines'.
 36846 The total width of the window is as returned by `window-total-width', it
 36847 includes the fringes, the continuation and truncation glyphs, the
 36848 display margins (if any), and the scroll bar
 36849 
 36850 For any other non-nil value, truncate lines in all windows that do
 36851 not span the full frame width.
 36852 
 36853 A value of nil means to respect the value of `truncate-lines'.
 36854 
 36855 If `word-wrap' is enabled, you might want to reduce the value of this.
 36856 
 36857 Don't set this to a non-nil value when `visual-line-mode' is
 36858 turned on, as it could produce confusing results.  */);
 36859   Vtruncate_partial_width_windows = make_fixnum (50);
 36860 
 36861   DEFVAR_BOOL("word-wrap-by-category", word_wrap_by_category, doc: /*
 36862     Non-nil means also wrap after characters of a certain category.
 36863 Normally when `word-wrap' is on, Emacs only breaks lines after
 36864 whitespace characters.  When this option is turned on, Emacs also
 36865 breaks lines after characters that have the "|" category (defined in
 36866 characters.el).  This is useful for allowing breaking after CJK
 36867 characters and improves the word-wrapping for CJK text mixed with
 36868 Latin text.
 36869 
 36870 If this variable is set using Customize, Emacs automatically loads
 36871 kinsoku.el.  When kinsoku.el is loaded, Emacs respects kinsoku rules
 36872 when breaking lines.  That means characters with the ">" category
 36873 don't appear at the beginning of a line (e.g., FULLWIDTH COMMA), and
 36874 characters with the "<" category don't appear at the end of a line
 36875 (e.g., LEFT DOUBLE ANGLE BRACKET).  */);
 36876   word_wrap_by_category = false;
 36877 
 36878   DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
 36879     doc: /* Maximum buffer size for which line number should be displayed.
 36880 If the buffer is bigger than this, the line number does not appear
 36881 in the mode line.  A value of nil means no limit.  */);
 36882   Vline_number_display_limit = Qnil;
 36883 
 36884   DEFVAR_INT ("line-number-display-limit-width",
 36885               line_number_display_limit_width,
 36886     doc: /* Maximum line width (in characters) for line number display.
 36887 If the average length of the lines near point is bigger than this, then the
 36888 line number may be omitted from the mode line.  */);
 36889   line_number_display_limit_width = 200;
 36890 
 36891   DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
 36892     doc: /* Non-nil means highlight active region even in nonselected windows.
 36893 When nil (the default), the active region is only highlighted when
 36894 the window is selected.  */);
 36895   highlight_nonselected_windows = false;
 36896 
 36897   DEFVAR_BOOL ("multiple-frames", multiple_frames,
 36898     doc: /* Non-nil if more than one frame is visible on this display.
 36899 Minibuffer-only frames don't count, but iconified frames do.
 36900 This variable is not guaranteed to be accurate except while processing
 36901 `frame-title-format' and `icon-title-format'.  */);
 36902 
 36903   DEFVAR_LISP ("frame-title-format", Vframe_title_format,
 36904     doc: /* Template for displaying the title bar of visible frames.
 36905 \(Assuming the window manager supports this feature.)
 36906 
 36907 This variable has the same structure as `mode-line-format', except that
 36908 the %c, %C, and %l constructs are ignored.  It is used only on frames for
 36909 which no explicit name has been set (see `modify-frame-parameters').  */);
 36910 
 36911   DEFVAR_LISP ("icon-title-format", Vicon_title_format,
 36912     doc: /* Template for displaying the title bar of an iconified frame.
 36913 \(Assuming the window manager supports this feature.)
 36914 If the value is a string, it should have the same structure
 36915 as `mode-line-format' (which see), and is used only on frames
 36916 for which no explicit name has been set \(see `modify-frame-parameters').
 36917 If the value is t, that means use `frame-title-format' for
 36918 iconified frames.  */);
 36919   /* Do not nest calls to pure_list.  This works around a bug in
 36920      Oracle Developer Studio 12.6.  */
 36921   Lisp_Object icon_title_name_format
 36922     = pure_list (empty_unibyte_string,
 36923                  build_pure_c_string ("%b - GNU Emacs at "),
 36924                  intern_c_string ("system-name"));
 36925   Vicon_title_format
 36926     = Vframe_title_format
 36927     = pure_list (intern_c_string ("multiple-frames"),
 36928                  build_pure_c_string ("%b"),
 36929                  icon_title_name_format);
 36930 
 36931   DEFVAR_LISP ("message-log-max", Vmessage_log_max,
 36932     doc: /* Maximum number of lines to keep in the message log buffer.
 36933 If nil, disable message logging.  If t, log messages but don't truncate
 36934 the buffer when it becomes large.  */);
 36935   Vmessage_log_max = make_fixnum (1000);
 36936 
 36937   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
 36938     doc: /* List of functions to call before redisplaying a window with scrolling.
 36939 Each function is called with two arguments, the window and its new
 36940 display-start position.
 36941 These functions are called whenever the `window-start' marker is modified,
 36942 either to point into another buffer (e.g. via `set-window-buffer') or another
 36943 place in the same buffer.
 36944 When each function is called, the `window-start' marker of its window
 36945 argument has been already set to the new value, and the buffer which that
 36946 window will display is set to be the current buffer.
 36947 Note that the value of `window-end' is not valid when these functions are
 36948 called.
 36949 
 36950 Warning: Do not use this feature to alter the way the window
 36951 is scrolled.  It is not designed for that, and such use probably won't
 36952 work.  */);
 36953   Vwindow_scroll_functions = Qnil;
 36954 
 36955   DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
 36956      doc: /* Non-nil means autoselect window with mouse pointer.
 36957 If nil, do not autoselect windows.
 36958 A positive number means delay autoselection by that many seconds: a
 36959 window is autoselected only after the mouse has remained in that
 36960 window for the duration of the delay.
 36961 A negative number has a similar effect, but causes windows to be
 36962 autoselected only after the mouse has stopped moving.  (Because of
 36963 the way Emacs compares mouse events, you will occasionally wait twice
 36964 that time before the window gets selected.)
 36965 Any other value means to autoselect window instantaneously when the
 36966 mouse pointer enters it.
 36967 
 36968 Autoselection selects the minibuffer only if it is active, and never
 36969 unselects the minibuffer if it is active.
 36970 
 36971 If you want to use the mouse to autoselect a window on another frame,
 36972 make sure that (1) your window manager has focus follow the mouse and
 36973 (2) the value of the option `focus-follows-mouse' matches the policy
 36974 of your window manager.  */);
 36975   Vmouse_autoselect_window = Qnil;
 36976 
 36977   DEFVAR_LISP ("auto-resize-tab-bars", Vauto_resize_tab_bars,
 36978     doc: /* Non-nil means automatically resize tab-bars.
 36979 This dynamically changes the tab-bar's height to the minimum height
 36980 that is needed to make all tab-bar items visible.
 36981 If value is `grow-only', the tab-bar's height is only increased
 36982 automatically; to decrease the tab-bar height, use \\[recenter],
 36983 after setting `recenter-redisplay' to the value of t.  */);
 36984   Vauto_resize_tab_bars = Qt;
 36985 
 36986   DEFVAR_BOOL ("auto-raise-tab-bar-buttons", auto_raise_tab_bar_buttons_p,
 36987     doc: /* Non-nil means raise tab-bar buttons when the mouse moves over them.  */);
 36988   auto_raise_tab_bar_buttons_p = true;
 36989 
 36990   DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
 36991     doc: /* Non-nil means automatically resize tool-bars.
 36992 This dynamically changes the tool-bar's height to the minimum height
 36993 that is needed to make all tool-bar items visible.
 36994 If value is `grow-only', the tool-bar's height is only increased
 36995 automatically; to decrease the tool-bar height, use \\[recenter],
 36996 after setting `recenter-redisplay' to the value of t.  */);
 36997   Vauto_resize_tool_bars = Qt;
 36998 
 36999   DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
 37000     doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them.  */);
 37001   auto_raise_tool_bar_buttons_p = true;
 37002 
 37003   DEFVAR_LISP ("make-cursor-line-fully-visible", Vmake_cursor_line_fully_visible,
 37004     doc: /* Whether to scroll the window if the cursor line is not fully visible.
 37005 If the value is non-nil, Emacs scrolls or recenters the window to make
 37006 the cursor line fully visible.  The value could also be a function, which
 37007 is called with a single argument, the window to be scrolled, and should
 37008 return non-nil if the partially-visible cursor requires scrolling the
 37009 window, nil if it's okay to leave the cursor partially-visible.  */);
 37010   Vmake_cursor_line_fully_visible = Qt;
 37011   DEFSYM (Qmake_cursor_line_fully_visible, "make-cursor-line-fully-visible");
 37012 
 37013   DEFVAR_BOOL ("make-window-start-visible", make_window_start_visible,
 37014     doc: /* Whether to ensure `window-start' position is never invisible.  */);
 37015   make_window_start_visible = false;
 37016   DEFSYM (Qmake_window_start_visible, "make-window-start-visible");
 37017   Fmake_variable_buffer_local (Qmake_window_start_visible);
 37018 
 37019   DEFSYM (Qclose_tab, "close-tab");
 37020   DEFVAR_LISP ("tab-bar-border", Vtab_bar_border,
 37021     doc: /* Border below tab-bar in pixels.
 37022 If an integer, use it as the height of the border.
 37023 If it is one of `internal-border-width' or `border-width', use the
 37024 value of the corresponding frame parameter.
 37025 Otherwise, no border is added below the tab-bar.  */);
 37026   Vtab_bar_border = Qinternal_border_width;
 37027 
 37028   DEFVAR_LISP ("tab-bar-button-margin", Vtab_bar_button_margin,
 37029     doc: /* Margin around tab-bar buttons in pixels.
 37030 If an integer, use that for both horizontal and vertical margins.
 37031 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 37032 HORZ specifying the horizontal margin, and VERT specifying the
 37033 vertical margin.  */);
 37034   Vtab_bar_button_margin = make_fixnum (DEFAULT_TAB_BAR_BUTTON_MARGIN);
 37035 
 37036   DEFVAR_INT ("tab-bar-button-relief", tab_bar_button_relief,
 37037     doc: /* Relief thickness of tab-bar buttons.  */);
 37038   tab_bar_button_relief = DEFAULT_TAB_BAR_BUTTON_RELIEF;
 37039 
 37040   DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
 37041     doc: /* Border below tool-bar in pixels.
 37042 If an integer, use it as the height of the border.
 37043 If it is one of `internal-border-width' or `border-width', use the
 37044 value of the corresponding frame parameter.
 37045 Otherwise, no border is added below the tool-bar.  */);
 37046   Vtool_bar_border = Qinternal_border_width;
 37047 
 37048   DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
 37049     doc: /* Margin around tool-bar buttons in pixels.
 37050 If an integer, use that for both horizontal and vertical margins.
 37051 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
 37052 HORZ specifying the horizontal margin, and VERT specifying the
 37053 vertical margin.  */);
 37054   Vtool_bar_button_margin = make_fixnum (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
 37055 
 37056   DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
 37057     doc: /* Relief thickness of tool-bar buttons.  */);
 37058   tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
 37059 
 37060   DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
 37061     doc: /* Tool bar style to use.
 37062 It can be one of
 37063  image            - show images only
 37064  text             - show text only
 37065  both             - show both, text below image
 37066  both-horiz       - show text to the right of the image
 37067  text-image-horiz - show text to the left of the image
 37068  any other        - use system default or image if no system default.
 37069 
 37070 This variable only affects the GTK+ toolkit version of Emacs.  */);
 37071   Vtool_bar_style = Qnil;
 37072 
 37073   DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
 37074     doc: /* Maximum number of characters a label can have to be shown.
 37075 The tool bar style must also show labels for this to have any effect, see
 37076 `tool-bar-style'.  */);
 37077   tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
 37078 
 37079   DEFVAR_LISP ("fontification-functions", Vfontification_functions,
 37080     doc: /* List of functions to call to fontify regions of text.
 37081 Each function is called with one argument POS.  Functions must
 37082 fontify a region starting at POS in the current buffer, and give
 37083 fontified regions the property `fontified' with a non-nil value.
 37084 
 37085 Note that, when `long-line-optimizations-p' is non-nil in the buffer,
 37086 these functions are called as if they were in a `with-restriction' form,
 37087 with a `long-line-optimizations-in-fontification-functions' label and
 37088 with the buffer narrowed to a portion around POS whose size is
 37089 specified by `long-line-optimizations-region-size'.  */);
 37090   Vfontification_functions = Qnil;
 37091   Fmake_variable_buffer_local (Qfontification_functions);
 37092 
 37093   DEFVAR_BOOL ("unibyte-display-via-language-environment",
 37094                unibyte_display_via_language_environment,
 37095     doc: /* Non-nil means display unibyte text according to language environment.
 37096 Specifically, this means that raw bytes in the range 160-255 decimal
 37097 are displayed by converting them to the equivalent multibyte characters
 37098 according to the current language environment.  As a result, they are
 37099 displayed according to the current fontset.
 37100 
 37101 Note that this variable affects only how these bytes are displayed,
 37102 but does not change the fact they are interpreted as raw bytes.  */);
 37103   unibyte_display_via_language_environment = false;
 37104 
 37105   DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
 37106     doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
 37107 If a float, it specifies the maximum height in units of the
 37108 mini-window frame's height.
 37109 If an integer, it specifies the maximum height in units of the
 37110 mini-window frame's default font's height.  */);
 37111   Vmax_mini_window_height = make_float (0.25);
 37112 
 37113   DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
 37114     doc: /* How to resize mini-windows (the minibuffer and the echo area).
 37115 A value of nil means don't automatically resize mini-windows.
 37116 A value of t means resize them to fit the text displayed in them.
 37117 A value of `grow-only', the default, means let mini-windows grow only;
 37118 they return to their normal size when the minibuffer is closed, or the
 37119 echo area becomes empty.
 37120 
 37121 This variable does not affect resizing of the minibuffer window of
 37122 minibuffer-only frames.  These are handled by `resize-mini-frames'
 37123 only.  */);
 37124   /* Contrary to the doc string, we initialize this to nil, so that
 37125      loading loadup.el won't try to resize windows before loading
 37126      window.el, where some functions we need to call for this live.
 37127      We assign the 'grow-only' value right after loading window.el
 37128      during loadup.  */
 37129   Vresize_mini_windows = Qnil;
 37130 
 37131   DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
 37132     doc: /* Alist specifying how to blink the cursor off.
 37133 Each element has the form (ON-STATE . OFF-STATE).  Whenever the
 37134 `cursor-type' frame-parameter or variable equals ON-STATE,
 37135 comparing using `equal', Emacs uses OFF-STATE to specify
 37136 how to blink it off.  ON-STATE and OFF-STATE are values for
 37137 the `cursor-type' frame parameter.
 37138 
 37139 If a frame's ON-STATE has no entry in this list,
 37140 the frame's other specifications determine how to blink the cursor off.  */);
 37141   Vblink_cursor_alist = Qnil;
 37142 
 37143   DEFVAR_LISP ("auto-hscroll-mode", automatic_hscrolling,
 37144     doc: /* Allow or disallow automatic horizontal scrolling of windows.
 37145 The value `current-line' means the line displaying point in each window
 37146 is automatically scrolled horizontally to make point visible.
 37147 Any other non-nil value means all the lines in a window are automatically
 37148 scrolled horizontally to make point visible.  */);
 37149   automatic_hscrolling = Qt;
 37150   DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
 37151   DEFSYM (Qcurrent_line, "current-line");
 37152 
 37153   DEFVAR_INT ("hscroll-margin", hscroll_margin,
 37154     doc: /* How many columns away from the window edge point is allowed to get
 37155 before automatic hscrolling will horizontally scroll the window.  */);
 37156   hscroll_margin = 5;
 37157 
 37158   DEFVAR_LISP ("hscroll-step", Vhscroll_step,
 37159     doc: /* How many columns to scroll the window when point gets too close to the edge.
 37160 When point is less than `hscroll-margin' columns from the window
 37161 edge, automatic hscrolling will scroll the window by the amount of columns
 37162 determined by this variable.  If its value is a positive integer, scroll that
 37163 many columns.  If it's a positive floating-point number, it specifies the
 37164 fraction of the window's width to scroll.  If it's nil or zero, point will be
 37165 centered horizontally after the scroll.  Any other value, including negative
 37166 numbers, are treated as if the value were zero.
 37167 
 37168 Automatic hscrolling always moves point outside the scroll margin, so if
 37169 point was more than scroll step columns inside the margin, the window will
 37170 scroll more than the value given by the scroll step.
 37171 
 37172 Note that the lower bound for automatic hscrolling specified by `scroll-left'
 37173 and `scroll-right' overrides this variable's effect.  */);
 37174   Vhscroll_step = make_fixnum (0);
 37175 
 37176   DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
 37177     doc: /* If non-nil, messages are truncated when displaying the echo area.
 37178 Bind this around calls to `message' to let it take effect.  */);
 37179   message_truncate_lines = false;
 37180 
 37181   DEFVAR_LISP ("menu-bar-update-hook",  Vmenu_bar_update_hook,
 37182     doc: /* Normal hook run to update the menu bar definitions.
 37183 Redisplay runs this hook before it redisplays the menu bar.
 37184 This is used to update menus such as Buffers, whose contents depend on
 37185 various data.  */);
 37186   Vmenu_bar_update_hook = Qnil;
 37187 
 37188   DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
 37189                doc: /* Frame for which we are updating a menu.
 37190 The enable predicate for a menu binding should check this variable.  */);
 37191   Vmenu_updating_frame = Qnil;
 37192 
 37193   DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
 37194     doc: /* Non-nil means don't update menu bars.  Internal use only.  */);
 37195   inhibit_menubar_update = false;
 37196 
 37197   DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
 37198     doc: /* Prefix prepended to all continuation lines at display time.
 37199 The value may be a string, an image, or a stretch-glyph; it is
 37200 interpreted in the same way as the value of a `display' text property.
 37201 
 37202 This variable is overridden by any `wrap-prefix' text or overlay
 37203 property.
 37204 
 37205 To add a prefix to non-continuation lines, use `line-prefix'.  */);
 37206   Vwrap_prefix = Qnil;
 37207   DEFSYM (Qwrap_prefix, "wrap-prefix");
 37208   Fmake_variable_buffer_local (Qwrap_prefix);
 37209 
 37210   DEFVAR_LISP ("line-prefix", Vline_prefix,
 37211     doc: /* Prefix prepended to all non-continuation lines at display time.
 37212 The value may be a string, an image, or a stretch-glyph; it is
 37213 interpreted in the same way as the value of a `display' text property.
 37214 
 37215 This variable is overridden by any `line-prefix' text or overlay
 37216 property.
 37217 
 37218 To add a prefix to continuation lines, use `wrap-prefix'.  */);
 37219   Vline_prefix = Qnil;
 37220   DEFSYM (Qline_prefix, "line-prefix");
 37221   Fmake_variable_buffer_local (Qline_prefix);
 37222 
 37223   DEFVAR_LISP ("display-line-numbers", Vdisplay_line_numbers,
 37224     doc: /* Non-nil means display line numbers.
 37225 
 37226 If the value is t, display the absolute number of each line of a buffer
 37227 shown in a window.  Absolute line numbers count from the beginning of
 37228 the current narrowing, or from buffer beginning.  The variable
 37229 `display-line-numbers-offset', if non-zero, is a signed offset added
 37230 to each absolute line number; it also forces line numbers to be counted
 37231 from the beginning of the buffer, as if `display-line-numbers-widen'
 37232 were non-nil.  It has no effect when line numbers are not absolute.
 37233 
 37234 If the value is `relative', display for each line not containing the
 37235 window's point its relative number instead, i.e. the number of the line
 37236 relative to the line showing the window's point.
 37237 
 37238 In either case, line numbers are displayed at the beginning of each
 37239 non-continuation line that displays buffer text, i.e. after each newline
 37240 character that comes from the buffer.  The value `visual' is like
 37241 `relative' but counts screen lines instead of buffer lines.  In practice
 37242 this means that continuation lines count as well when calculating the
 37243 relative number of a line.
 37244 
 37245 Lisp programs can disable display of a line number of a particular
 37246 buffer line by putting the `display-line-numbers-disable' text property
 37247 or overlay property on the first visible character of that line.  */);
 37248   Vdisplay_line_numbers = Qnil;
 37249   DEFSYM (Qdisplay_line_numbers, "display-line-numbers");
 37250   Fmake_variable_buffer_local (Qdisplay_line_numbers);
 37251   DEFSYM (Qrelative, "relative");
 37252   DEFSYM (Qvisual, "visual");
 37253 
 37254   DEFVAR_LISP ("display-line-numbers-width", Vdisplay_line_numbers_width,
 37255     doc: /* Minimum width of space reserved for line number display.
 37256 A positive number means reserve that many columns for line numbers,
 37257 even if the actual number needs less space.
 37258 The default value of nil means compute the space dynamically.
 37259 Any other value is treated as nil.  */);
 37260   Vdisplay_line_numbers_width = Qnil;
 37261   DEFSYM (Qdisplay_line_numbers_width, "display-line-numbers-width");
 37262   Fmake_variable_buffer_local (Qdisplay_line_numbers_width);
 37263 
 37264   DEFVAR_LISP ("display-line-numbers-current-absolute",
 37265                Vdisplay_line_numbers_current_absolute,
 37266     doc: /* Non-nil means display absolute number of current line.
 37267 This variable has effect only when `display-line-numbers' is
 37268 either `relative' or `visual'.  */);
 37269   Vdisplay_line_numbers_current_absolute = Qt;
 37270 
 37271   DEFVAR_BOOL ("display-line-numbers-widen", display_line_numbers_widen,
 37272     doc: /* Non-nil means display line numbers disregarding any narrowing.  */);
 37273   display_line_numbers_widen = false;
 37274   DEFSYM (Qdisplay_line_numbers_widen, "display-line-numbers-widen");
 37275   Fmake_variable_buffer_local (Qdisplay_line_numbers_widen);
 37276 
 37277   DEFVAR_INT ("display-line-numbers-offset", display_line_numbers_offset,
 37278     doc: /* A signed integer added to each absolute line number.
 37279 When this variable is non-zero, line numbers are always counted from
 37280 the beginning of the buffer even if `display-line-numbers-widen' is nil.
 37281 It has no effect when set to 0, or when line numbers are not absolute.  */);
 37282   display_line_numbers_offset = 0;
 37283   DEFSYM (Qdisplay_line_numbers_offset, "display-line-numbers-offset");
 37284   Fmake_variable_buffer_local (Qdisplay_line_numbers_offset);
 37285 
 37286   DEFVAR_BOOL ("display-fill-column-indicator", display_fill_column_indicator,
 37287     doc: /* Non-nil means display the fill column indicator.
 37288 If you set this non-nil, make sure `display-fill-column-indicator-character'
 37289 is also non-nil.
 37290 See Info node `Displaying Boundaries' for details.  */);
 37291   display_fill_column_indicator = false;
 37292   DEFSYM (Qdisplay_fill_column_indicator, "display-fill-column-indicator");
 37293   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator);
 37294 
 37295   DEFVAR_LISP ("display-fill-column-indicator-column", Vdisplay_fill_column_indicator_column,
 37296     doc: /* Column for indicator when `display-fill-column-indicator' is non-nil.
 37297 The default value is t which means that the indicator
 37298 will use the `fill-column' variable.  If it is set to an integer the
 37299 indicator will be drawn in that column.
 37300 See Info node `Displaying Boundaries' for details.  */);
 37301   Vdisplay_fill_column_indicator_column = Qt;
 37302   DEFSYM (Qdisplay_fill_column_indicator_column, "display-fill-column-indicator-column");
 37303   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_column);
 37304 
 37305   DEFVAR_LISP ("display-fill-column-indicator-character", Vdisplay_fill_column_indicator_character,
 37306     doc: /* Character to draw the indicator when `display-fill-column-indicator' is non-nil.
 37307 A good candidate is U+2502, and an alternative is (ascii 124) if the
 37308 font of `fill-column-indicator' face does not support Unicode characters.
 37309 See Info node `Displaying Boundaries' for details.  */);
 37310   Vdisplay_fill_column_indicator_character = Qnil;
 37311   DEFSYM (Qdisplay_fill_column_indicator_character, "display-fill-column-indicator-character");
 37312   Fmake_variable_buffer_local (Qdisplay_fill_column_indicator_character);
 37313 
 37314   DEFVAR_INT ("display-line-numbers-major-tick", display_line_numbers_major_tick,
 37315     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37316 The line number is shown with the `line-number-major-tick' face.
 37317 Otherwise, no special highlighting is done every Nth line.
 37318 Note that major ticks take precedence over minor ticks.  */);
 37319   display_line_numbers_major_tick = 0;
 37320 
 37321   DEFVAR_INT ("display-line-numbers-minor-tick", display_line_numbers_minor_tick,
 37322     doc: /* If an integer N > 0, highlight line number of every Nth line.
 37323 The line number is shown with the `line-number-minor-tick' face.
 37324 Otherwise, no special highlighting is done every Nth line.
 37325 Note that major ticks take precedence over minor ticks.  */);
 37326   display_line_numbers_minor_tick = 0;
 37327 
 37328   DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
 37329     doc: /* Non-nil means don't eval Lisp during redisplay.  */);
 37330   inhibit_eval_during_redisplay = false;
 37331 
 37332   DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
 37333     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
 37334   inhibit_free_realized_faces = false;
 37335 
 37336   DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
 37337     doc: /* Non-nil means don't mirror characters even when bidi context requires that.
 37338 Intended for use during debugging and for testing bidi display;
 37339 see biditest.el in the test suite.  */);
 37340   inhibit_bidi_mirroring = false;
 37341 
 37342   DEFVAR_BOOL ("bidi-inhibit-bpa", bidi_inhibit_bpa,
 37343     doc: /* Non-nil means inhibit the Bidirectional Parentheses Algorithm.
 37344 Disabling the BPA makes redisplay faster, but might produce incorrect
 37345 display reordering of bidirectional text with embedded parentheses and
 37346 other bracket characters whose `paired-bracket' Unicode property is
 37347 non-nil, see `get-char-code-property'.  */);
 37348   bidi_inhibit_bpa = false;
 37349 
 37350 #ifdef GLYPH_DEBUG
 37351   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
 37352                doc: /* Inhibit try_window_id display optimization.  */);
 37353   inhibit_try_window_id = false;
 37354 
 37355   DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
 37356                doc: /* Inhibit try_window_reusing display optimization.  */);
 37357   inhibit_try_window_reusing = false;
 37358 
 37359   DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
 37360                doc: /* Inhibit try_cursor_movement display optimization.  */);
 37361   inhibit_try_cursor_movement = false;
 37362 #endif /* GLYPH_DEBUG */
 37363 
 37364   DEFVAR_INT ("overline-margin", overline_margin,
 37365                doc: /* Space between overline and text, in pixels.
 37366 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
 37367 margin to the character height.  */);
 37368   overline_margin = 2;
 37369 
 37370   DEFVAR_INT ("underline-minimum-offset",
 37371                underline_minimum_offset,
 37372      doc: /* Minimum distance between baseline and underline.
 37373 This can improve legibility of underlined text at small font sizes,
 37374 particularly when using variable `x-use-underline-position-properties'
 37375 with fonts that specify an UNDERLINE_POSITION relatively close to the
 37376 baseline.  The default value is 1.  */);
 37377   underline_minimum_offset = 1;
 37378   DEFSYM (Qunderline_minimum_offset, "underline-minimum-offset");
 37379 
 37380   DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
 37381                doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
 37382 This feature only works when on a window system that can change
 37383 cursor shapes.  */);
 37384   display_hourglass_p = true;
 37385 
 37386   DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
 37387                doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy.  */);
 37388   Vhourglass_delay = make_fixnum (DEFAULT_HOURGLASS_DELAY);
 37389 
 37390 #ifdef HAVE_WINDOW_SYSTEM
 37391   hourglass_atimer = NULL;
 37392   hourglass_shown_p = false;
 37393 #endif /* HAVE_WINDOW_SYSTEM */
 37394 
 37395   /* Name of the face used to display glyphless characters.  */
 37396   DEFSYM (Qglyphless_char, "glyphless-char");
 37397 
 37398   /* Method symbols for Vglyphless_char_display.  */
 37399   DEFSYM (Qhex_code, "hex-code");
 37400   DEFSYM (Qempty_box, "empty-box");
 37401   DEFSYM (Qthin_space, "thin-space");
 37402   DEFSYM (Qzero_width, "zero-width");
 37403 
 37404   DEFVAR_LISP ("pre-redisplay-function", Vpre_redisplay_function,
 37405                doc: /* Function run just before redisplay.
 37406 It is called with one argument, which is the set of windows that are to
 37407 be redisplayed.  This set can be nil (meaning, only the selected window),
 37408 or t (meaning all windows).  */);
 37409   Vpre_redisplay_function = intern ("ignore");
 37410 
 37411   /* Symbol for the purpose of Vglyphless_char_display.  */
 37412   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
 37413   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_fixnum (1));
 37414 
 37415   DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
 37416                doc: /* Char-table defining glyphless characters.
 37417 Each element, if non-nil, should be one of the following:
 37418   an ASCII acronym string: display this string in a box
 37419   `hex-code':   display the hexadecimal code of a character in a box
 37420   `empty-box':  display as an empty box
 37421   `thin-space': display as 1-pixel width space
 37422   `zero-width': don't display
 37423 Any other value is interpreted as `empty-box'.
 37424 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
 37425 display method for graphical terminals and text terminals respectively.
 37426 GRAPHICAL and TEXT should each have one of the values listed above.
 37427 
 37428 The char-table has one extra slot to control the display of characters
 37429 for which no font is found on graphical terminals, and characters that
 37430 cannot be displayed by text-mode terminals.  Its value should be an
 37431 ASCII acronym string, `hex-code', `empty-box', or `thin-space'.  It
 37432 could also be a cons cell of any two of these, to specify separate
 37433 values for graphical and text terminals.  The default is `empty-box'.
 37434 
 37435 With the obvious exception of `zero-width', all the other representations
 37436 are displayed using the face `glyphless-char'.
 37437 
 37438 If a character has a non-nil entry in an active display table, the
 37439 display table takes effect; in this case, Emacs does not consult
 37440 `glyphless-char-display' at all.  */);
 37441   Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
 37442   Fset_char_table_extra_slot (Vglyphless_char_display, make_fixnum (0),
 37443                               Qempty_box);
 37444 
 37445   DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
 37446                doc: /* If non-nil, debug if a message matching this regexp is displayed.  */);
 37447   Vdebug_on_message = Qnil;
 37448 
 37449   DEFVAR_LISP ("set-message-function", Vset_message_function,
 37450                doc: /* If non-nil, function to handle display of echo-area messages.
 37451 The function is called with one argument that is the text of a message.
 37452 If this function returns nil, the message is displayed in the echo area
 37453 as usual.  If the function returns a string, the returned string is
 37454 displayed in the echo area.  If this function returns any other non-nil
 37455 value, this means that the message was already handled, and the original
 37456 message text will not be displayed in the echo area.
 37457 
 37458 Also see `clear-message-function' (which can be used to clear the
 37459 message displayed by this function), and `command-error-function'
 37460 (which controls how error messages are displayed).  */);
 37461   Vset_message_function = Qnil;
 37462 
 37463   DEFSYM (Qdont_clear_message, "dont-clear-message");
 37464   DEFVAR_LISP ("clear-message-function", Vclear_message_function,
 37465                doc: /* If non-nil, function to clear echo-area messages.
 37466 Usually this function is called when the next input event arrives.
 37467 It is expected to clear the message displayed by its counterpart
 37468 function specified by `set-message-function'.
 37469 
 37470 The function is called without arguments.
 37471 
 37472 If this function returns a value that isn't `dont-clear-message', the
 37473 message is cleared from the echo area as usual.  If this function
 37474 returns `dont-clear-message', this means that the message was already
 37475 handled, and the original message text will not be cleared from the
 37476 echo area.  */);
 37477   Vclear_message_function = Qnil;
 37478 
 37479   DEFVAR_LISP ("redisplay--all-windows-cause", Vredisplay__all_windows_cause,
 37480                doc: /* Code of the cause for redisplaying all windows.
 37481 Internal use only.  */);
 37482   Vredisplay__all_windows_cause = Fmake_hash_table (0, NULL);
 37483 
 37484   DEFVAR_LISP ("redisplay--mode-lines-cause", Vredisplay__mode_lines_cause,
 37485                doc: /* Code of the cause for redisplaying mode lines.
 37486 Internal use only.  */);
 37487   Vredisplay__mode_lines_cause = Fmake_hash_table (0, NULL);
 37488 
 37489   DEFVAR_BOOL ("redisplay--inhibit-bidi", redisplay__inhibit_bidi,
 37490      doc: /* Non-nil means it is not safe to attempt bidi reordering for display.  */);
 37491   /* Initialize to t, since we need to disable reordering until
 37492      loadup.el successfully loads charprop.el.  */
 37493   redisplay__inhibit_bidi = true;
 37494 
 37495   DEFVAR_BOOL ("display-raw-bytes-as-hex", display_raw_bytes_as_hex,
 37496     doc: /* Non-nil means display raw bytes in hexadecimal format.
 37497 The default is to use octal format (\\200) whereas hexadecimal (\\x80)
 37498 may be more familiar to users.  */);
 37499   display_raw_bytes_as_hex = false;
 37500 
 37501   DEFVAR_BOOL ("mouse-fine-grained-tracking", mouse_fine_grained_tracking,
 37502     doc: /* Non-nil for pixel-wise mouse-movement.
 37503 When nil, mouse-movement events will not be generated as long as the
 37504 mouse stays within the extent of a single glyph (except for images).  */);
 37505   mouse_fine_grained_tracking = false;
 37506 
 37507   DEFVAR_BOOL ("tab-bar--dragging-in-progress", tab_bar__dragging_in_progress,
 37508     doc: /* Non-nil when maybe dragging tab bar item.  */);
 37509   tab_bar__dragging_in_progress = false;
 37510 
 37511   DEFVAR_BOOL ("redisplay-skip-initial-frame", redisplay_skip_initial_frame,
 37512     doc: /* Non-nil means skip redisplay of the initial frame.
 37513 The initial frame is the text-mode frame used by Emacs internally during
 37514 the early stages of startup.  That frame is not displayed anywhere, so
 37515 skipping it is best except in special circumstances such as running
 37516 redisplay tests in batch mode.   */);
 37517   redisplay_skip_initial_frame = true;
 37518 
 37519   DEFVAR_BOOL ("redisplay-skip-fontification-on-input",
 37520                redisplay_skip_fontification_on_input,
 37521     doc: /* Skip `fontification_functions` when there is input pending.
 37522 If non-nil and there was input pending at the beginning of the command,
 37523 the `fontification_functions` hook is not run.  This usually does not
 37524 affect the display because redisplay is completely skipped anyway if input
 37525 was pending, but it can make scrolling smoother by avoiding
 37526 unnecessary fontification.
 37527 It is similar to `fast-but-imprecise-scrolling' with similar tradeoffs,
 37528 but with the advantage that it should only affect the behavior when Emacs
 37529 has trouble keeping up with the incoming input rate.  */);
 37530   redisplay_skip_fontification_on_input = false;
 37531 
 37532   DEFVAR_BOOL ("redisplay-adhoc-scroll-in-resize-mini-windows",
 37533                redisplay_adhoc_scroll_in_resize_mini_windows,
 37534     doc: /* If nil always use normal scrolling in minibuffer windows.
 37535 Otherwise, use custom-tailored code after resizing minibuffer windows to try
 37536 and display the most important part of the minibuffer.   */);
 37537   /* See bug#43519 for some discussion around this.  */
 37538   redisplay_adhoc_scroll_in_resize_mini_windows = true;
 37539 
 37540   DEFVAR_BOOL ("composition-break-at-point", composition_break_at_point,
 37541     doc: /* If non-nil, prevent auto-composition of characters around point.
 37542 This makes it easier to edit character sequences that are
 37543 composed on display.  */);
 37544   composition_break_at_point = false;
 37545 
 37546   DEFVAR_INT ("max-redisplay-ticks", max_redisplay_ticks,
 37547     doc: /* Maximum number of redisplay ticks before aborting redisplay of a window.
 37548 
 37549 This allows to abort the display of a window if the amount of low-level
 37550 redisplay operations exceeds the value of this variable.  When display of
 37551 a window is aborted due to this reason, the buffer shown in that window
 37552 will not have its windows redisplayed until the buffer is modified or until
 37553 you type \\[recenter-top-bottom] with one of its windows selected.
 37554 You can also decide to kill the buffer and visit it in some
 37555 other way, like under `so-long-mode' or literally.
 37556 
 37557 The default value is zero, which disables this feature.
 37558 The recommended non-zero value is between 100000 and 1000000,
 37559 depending on your patience and the speed of your system.  */);
 37560   max_redisplay_ticks = 0;
 37561 }
 37562 
 37563 
 37564 /* Initialize this module when Emacs starts.  */
 37565 
 37566 void
 37567 init_xdisp (void)
 37568 {
 37569   CHARPOS (this_line_start_pos) = 0;
 37570 
 37571   echo_area_window = minibuf_window;
 37572 
 37573   if (!noninteractive)
 37574     {
 37575       struct window *m = XWINDOW (minibuf_window);
 37576       Lisp_Object frame = m->frame;
 37577       struct frame *f = XFRAME (frame);
 37578       Lisp_Object root = FRAME_ROOT_WINDOW (f);
 37579       struct window *r = XWINDOW (root);
 37580       int i;
 37581 
 37582       r->top_line = FRAME_TOP_MARGIN (f);
 37583       r->pixel_top = r->top_line * FRAME_LINE_HEIGHT (f);
 37584       r->total_cols = FRAME_COLS (f);
 37585       r->pixel_width = r->total_cols * FRAME_COLUMN_WIDTH (f);
 37586       r->total_lines = FRAME_TOTAL_LINES (f) - 1 - FRAME_MARGINS (f);
 37587       r->pixel_height = r->total_lines * FRAME_LINE_HEIGHT (f);
 37588 
 37589       m->top_line = FRAME_TOTAL_LINES (f) - 1;
 37590       m->pixel_top = m->top_line * FRAME_LINE_HEIGHT (f);
 37591       m->total_cols = FRAME_COLS (f);
 37592       m->pixel_width = m->total_cols * FRAME_COLUMN_WIDTH (f);
 37593       m->total_lines = 1;
 37594       m->pixel_height = m->total_lines * FRAME_LINE_HEIGHT (f);
 37595 
 37596       scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
 37597       scratch_glyph_row.glyphs[TEXT_AREA + 1]
 37598         = scratch_glyphs + MAX_SCRATCH_GLYPHS;
 37599 
 37600       /* The default ellipsis glyphs `...'.  */
 37601       for (i = 0; i < 3; ++i)
 37602         default_invis_vector[i] = make_fixnum ('.');
 37603     }
 37604 
 37605   {
 37606     /* Allocate the buffer for frame titles.
 37607        Also used for `format-mode-line'.  */
 37608     int size = 100;
 37609     mode_line_noprop_buf = xmalloc (size);
 37610     mode_line_noprop_buf_end = mode_line_noprop_buf + size;
 37611     mode_line_noprop_ptr = mode_line_noprop_buf;
 37612     mode_line_target = MODE_LINE_DISPLAY;
 37613   }
 37614 
 37615   help_echo_showing_p = false;
 37616 }
 37617 
 37618 #ifdef HAVE_WINDOW_SYSTEM
 37619 
 37620 /* Platform-independent portion of hourglass implementation.  */
 37621 
 37622 /* Timer function of hourglass_atimer.  */
 37623 
 37624 static void
 37625 show_hourglass (struct atimer *timer)
 37626 {
 37627   /* The timer implementation will cancel this timer automatically
 37628      after this function has run.  Set hourglass_atimer to null
 37629      so that we know the timer doesn't have to be canceled.  */
 37630   hourglass_atimer = NULL;
 37631 
 37632   if (!hourglass_shown_p)
 37633     {
 37634       Lisp_Object tail, frame;
 37635 
 37636       block_input ();
 37637 
 37638       FOR_EACH_FRAME (tail, frame)
 37639         {
 37640           struct frame *f = XFRAME (frame);
 37641 
 37642           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37643               && FRAME_RIF (f)->show_hourglass)
 37644             FRAME_RIF (f)->show_hourglass (f);
 37645         }
 37646 
 37647       hourglass_shown_p = true;
 37648       unblock_input ();
 37649     }
 37650 }
 37651 
 37652 /* Cancel a currently active hourglass timer, and start a new one.  */
 37653 
 37654 void
 37655 start_hourglass (void)
 37656 {
 37657   struct timespec delay;
 37658 
 37659   cancel_hourglass ();
 37660 
 37661   if (FIXNUMP (Vhourglass_delay)
 37662       && XFIXNUM (Vhourglass_delay) > 0)
 37663     delay = make_timespec (min (XFIXNUM (Vhourglass_delay),
 37664                                   TYPE_MAXIMUM (time_t)),
 37665                            0);
 37666   else if (FLOATP (Vhourglass_delay)
 37667            && XFLOAT_DATA (Vhourglass_delay) > 0)
 37668     delay = dtotimespec (XFLOAT_DATA (Vhourglass_delay));
 37669   else
 37670     delay = make_timespec (DEFAULT_HOURGLASS_DELAY, 0);
 37671 
 37672   hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
 37673                                    show_hourglass, NULL);
 37674 }
 37675 
 37676 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
 37677    shown.  */
 37678 
 37679 void
 37680 cancel_hourglass (void)
 37681 {
 37682   if (hourglass_atimer)
 37683     {
 37684       cancel_atimer (hourglass_atimer);
 37685       hourglass_atimer = NULL;
 37686     }
 37687 
 37688   if (hourglass_shown_p)
 37689     {
 37690       Lisp_Object tail, frame;
 37691 
 37692       block_input ();
 37693 
 37694       FOR_EACH_FRAME (tail, frame)
 37695         {
 37696           struct frame *f = XFRAME (frame);
 37697 
 37698           if (FRAME_LIVE_P (f) && FRAME_WINDOW_P (f)
 37699               && FRAME_RIF (f)->hide_hourglass)
 37700             FRAME_RIF (f)->hide_hourglass (f);
 37701 #ifdef HAVE_NTGUI
 37702           /* No cursors on non GUI frames - restore to stock arrow cursor.  */
 37703           else if (!FRAME_W32_P (f))
 37704             w32_arrow_cursor ();
 37705 #endif
 37706         }
 37707 
 37708       hourglass_shown_p = false;
 37709       unblock_input ();
 37710     }
 37711 }
 37712 
 37713 /* Return a correction to be applied to G->pixel_width when it is
 37714    displayed in MOUSE_FACE.  This is needed for the first and the last
 37715    glyphs of text inside a face with :box when it is displayed with
 37716    MOUSE_FACE that has a different or no :box attribute.
 37717    ORIGINAL_FACE is the face G was originally drawn in, and MOUSE_FACE
 37718    is the face it will be drawn in now.  ROW is the G's glyph row and
 37719    W is its window.  */
 37720 static int
 37721 adjust_glyph_width_for_mouse_face (struct glyph *g, struct glyph_row *row,
 37722                                    struct window *w,
 37723                                    struct face *original_face,
 37724                                    struct face *mouse_face)
 37725 {
 37726   int sum = 0;
 37727 
 37728   bool do_left_box_p = g->left_box_line_p;
 37729   bool do_right_box_p = g->right_box_line_p;
 37730 
 37731   /* This is required because we test some parameters of the image
 37732      slice before applying the box in produce_image_glyph.  */
 37733   if (g->type == IMAGE_GLYPH)
 37734     {
 37735       if (!row->reversed_p)
 37736         {
 37737           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37738                                              g->u.img_id);
 37739           do_left_box_p = g->left_box_line_p &&
 37740             g->slice.img.x == 0;
 37741           do_right_box_p = g->right_box_line_p &&
 37742             g->slice.img.x + g->slice.img.width == img->width;
 37743         }
 37744       else
 37745         {
 37746           struct image *img = IMAGE_FROM_ID (WINDOW_XFRAME (w),
 37747                                              g->u.img_id);
 37748           do_left_box_p = g->left_box_line_p &&
 37749             g->slice.img.x + g->slice.img.width == img->width;
 37750           do_right_box_p = g->right_box_line_p &&
 37751             g->slice.img.x == 0;
 37752         }
 37753     }
 37754 
 37755   /* If the glyph has a left box line, subtract it from the offset.  */
 37756   if (do_left_box_p)
 37757     sum -= max (0, original_face->box_vertical_line_width);
 37758   /* Likewise with the right box line, as there may be a
 37759      box there as well.  */
 37760   if (do_right_box_p)
 37761     sum -= max (0, original_face->box_vertical_line_width);
 37762   /* Now add the line widths from the new face.  */
 37763   if (g->left_box_line_p)
 37764     sum += max (0, mouse_face->box_vertical_line_width);
 37765   if (g->right_box_line_p)
 37766     sum += max (0, mouse_face->box_vertical_line_width);
 37767 
 37768   return sum;
 37769 }
 37770 
 37771 /* Get the offset due to mouse-highlight to apply before drawing
 37772    phys_cursor, and return it in OFFSET.  ROW should be the row that
 37773    is under mouse face and contains the phys cursor.
 37774 
 37775    This is required because the produce_XXX_glyph series of functions
 37776    add the width of the various vertical box lines to the total width
 37777    of the glyphs, but that must be updated when the row is put under
 37778    mouse face, which can have different box dimensions.  */
 37779 static void
 37780 get_cursor_offset_for_mouse_face (struct window *w, struct glyph_row *row,
 37781                                   int *offset)
 37782 {
 37783   int sum = 0;
 37784   /* Return because the mode line can't possibly have a cursor. */
 37785   if (row->mode_line_p)
 37786     return;
 37787 
 37788   block_input ();
 37789 
 37790   struct frame *f = WINDOW_XFRAME (w);
 37791   Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
 37792   struct glyph *start, *end;
 37793   struct face *mouse_face = FACE_FROM_ID (f, hlinfo->mouse_face_face_id);
 37794   int hpos = w->phys_cursor.hpos;
 37795   end = &row->glyphs[TEXT_AREA][hpos];
 37796 
 37797   if (!row->reversed_p)
 37798     {
 37799       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37800           hlinfo->mouse_face_beg_row)
 37801         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_beg_col];
 37802       else
 37803         start = row->glyphs[TEXT_AREA];
 37804     }
 37805   else
 37806     {
 37807       if (MATRIX_ROW_VPOS (row, w->current_matrix) ==
 37808           hlinfo->mouse_face_end_row)
 37809         start = &row->glyphs[TEXT_AREA][hlinfo->mouse_face_end_col];
 37810       else
 37811         start = &row->glyphs[TEXT_AREA][row->used[TEXT_AREA] - 1];
 37812     }
 37813 
 37814   /* Calculate the offset by which to correct phys_cursor x if we are
 37815      drawing the cursor inside mouse-face highlighted text.  */
 37816 
 37817   for ( ; row->reversed_p ? start > end : start < end;
 37818           row->reversed_p ? --start : ++start)
 37819     sum += adjust_glyph_width_for_mouse_face (start, row, w,
 37820                                               FACE_FROM_ID (f, start->face_id),
 37821                                               mouse_face);
 37822 
 37823   if (row->reversed_p)
 37824     sum = -sum;
 37825 
 37826   *offset = sum;
 37827 
 37828   unblock_input ();
 37829 }
 37830 #endif /* HAVE_WINDOW_SYSTEM */

/* [<][>][^][v][top][bottom][index][help] */